[Haskell] [newbye] 'Just a'
Arjun Guha
guhaarju at grinnell.edu
Sun Feb 6 00:02:59 EST 2005
The problem with your approach is that getAorB does not halt if the
list does not contain the first element. For example:
> getAorB 6 5 [5,5..]
While you may not care about a contrived example like this, it does
imply that your function scans the list once, searching for the first
element, and once again, searching for the second. Note that it scans
twice even if the first element was found, because of the pattern:
> f (Just a) Nothing = a
> f Nothing (Just a) = a
The following pattern behaves better:
> f (Just a) _ = a
> f _ (Just a) = a
and halts on:
> getAorB 5 6 [5,5..]
I don't think there is a way to get proper laziness by using get twice.
I suggest implementing getAorB using explicit recursion, which is
probably how you implemented get.
-Arjun
More information about the Haskell
mailing list