
On Sat, Jan 16, 2010 at 5:30 AM, Brandon S. Allbery KF8NH
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.
Good one, thanks. The reason I had them in there originally is because I was thinking it would be simpler if a function was passed in all the inputs it was working with -- instead of referring to variables defined outside its scope. Joe