[Haskell-cafe] foldlWhile

Jorge Adriano Aires jadrian at mat.uc.pt
Sat Nov 20 11:55:08 EST 2004


(opss just noticed I did a reply-to)

> > The following is closer to the original, but doesn't work when the whole
> > list is folded (i.e., p always satisfied):
> > foldlWhile f p a = head . dropWhile p . scanl f a
>
> Serge's version returns the last 'a' that satisfies 'p', while yours
Not really.

> returns the first 'a' that does not satisfy 'p'.  This should be an
> equivalent version:
Yeap, just like his, 

foldlWhile :: (a -> b -> a) -> (a -> Bool) -> a -> [b] -> a
foldlWhile    f                p              a    bs  =
<snip>
(_,     False) -> a
<snip>

It tests the accumulator with p, and returns it on "false".

J.A.


More information about the Haskell-Cafe mailing list