[Haskell-cafe] foldlWhile

Dylan Thurston dpt at bostoncoop.net
Mon Nov 22 02:17:37 EST 2004

On Mon, Nov 22, 2004 at 09:41:47AM +0300, Serge D. Mechveliani wrote:
> Yes. Thank you. 
> Inspecting Prelude, we find out that  scanl  expresses, mainly, 
> the thing.

Did you find a good replacement for the second function, foldWhileJust?

Come to think of it, the way to do that would be to turn the accumulator
to a function from Maybe a to Maybe a, like this:

import Maybe(fromJust,isJust)

foldWhileJust :: (a -> b -> Maybe a) -> a -> [b] -> a
foldWhileJust    f                      a =
  let f' :: Maybe a -> b -> Maybe a
      f' = maybe (const Nothing) f
  in fromJust . last . takeWhile isJust . scanl f' (Just a)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20041122/02755d6a/attachment.bin

More information about the Haskell-Cafe mailing list