
Is such a function familia to the Haskell users? foldlWhile :: (a -> b -> a) -> (a -> Bool) -> a -> [b] -> a foldlWhile f p a bs = case (bs, p a) of ([], _ ) -> a (_, False) -> a (b:bs', _ ) -> foldlWhile f p (f a b) bs' foldl does not seem to cover this. Example. Sum the list while the sum is less than bound: boundSum :: Integer -> [Integer] -> Integer boundSum b = foldlWhile (+) (< b) 0 Example: boundSum 100 $ map (^2) [1 ..] Is this reasonable? More `generic' variant: foldlWhileJust :: (a -> b -> Maybe a) -> a -> [b] -> a foldlWhileJust f a bs = case bs of [] -> a b:bs' -> case f a b of Just a' -> foldlWhileJust f a' bs' _ -> a Please, copy the replies to mechvel@botik.ru ----------------- Serge Mechveliani mechvel@botik.ru

On Sat, Nov 20, 2004 at 12:47:58PM +0300, Serge D. Mechveliani wrote:
Is such a function familia to the Haskell users?
foldlWhile :: (a -> b -> a) -> (a -> Bool) -> a -> [b] -> a foldlWhile f p a bs = case (bs, p a) of ([], _ ) -> a (_, False) -> a (b:bs', _ ) -> foldlWhile f p (f a b) bs'
foldl does not seem to cover this.
Why not just foldlWhile f p a bs = takeWhile p $ foldl f a bs ?
More `generic' variant:
foldlWhileJust :: (a -> b -> Maybe a) -> a -> [b] -> a foldlWhileJust f a bs = case bs of
[] -> a b:bs' -> case f a b of Just a' -> foldlWhileJust f a' bs' _ -> a
I don't know a short way to rewrite this one yet. Peace, Dylan

On Saturday 20 November 2004 10:47, Serge D. Mechveliani wrote:
Is such a function familia to the Haskell users?
foldlWhile :: (a -> b -> a) -> (a -> Bool) -> a -> [b] -> a
Maybe this link is of interest to you: http://okmij.org/ftp/Haskell/#fold-stream Ben -- Top level things with identity are evil. -- Lennart Augustsson
participants (3)
-
Benjamin Franksen
-
dpt@lotus.bostoncoop.net
-
Serge D. Mechveliani