[Haskell-beginners] Re: Code help requested
Joe Van Dyk
joe at fixieconsulting.com
Sat Jan 16 15:09:53 EST 2010
On Sat, Jan 16, 2010 at 5:30 AM, Brandon S. Allbery KF8NH
<allbery at ece.cmu.edu> wrote:
> 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
More information about the Beginners
mailing list