
On Jan 15, 2010, at 20:17 , Tim Perry wrote:
binary_find list elem = do_search list elem 0 (length list -1) where do_search list elem low high | high < low = Nothing | midVal > elem = do_search list elem low (mid - 1) | midVal < elem = do_search list elem (mid + 1) high | otherwise = Just mid where midVal = list !! mid mid = low + (high - low) `div` 2
Observation: the first two parameters to do_search never change within an invocation of binary_find, and the corresponding arguments to binary_find are in scope; depending on the (lack of) cleverness of the compiler, you could see a speedup by not passing them around unnecessarily. In addition, it's *conceptually* cleaner because passing them around explicitly suggests to someone reading the source that they *do* change when that isn't actually the case. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH