[Haskell-cafe] how to break foldl' ?

Udo Stenzel u.stenzel at web.de
Fri Sep 30 15:23:59 EDT 2005


gary ng wrote:
> Still a bit confused.
> 
> My function is simply
> 
> f x y = if x < 100 then x + y else x

Try this:

sum_to_100 xs = foldr (\x k a -> if a < 100 then k $! (x+a) else a) id xs 0

As expected, (sum_to_100 [1..]) gives 105, without trying to find the
end of an infinite list.  You get your early-out semantics combined with
strict evaluation.


Udo.
-------------- 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/20050930/03ed4d60/attachment.bin


More information about the Haskell-Cafe mailing list