[Haskell-beginners] Re: Code help requested

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Sat Jan 16 08:30:04 EST 2010


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 at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/beginners/attachments/20100116/acd280c2/PGP.bin


More information about the Beginners mailing list