[Haskell-cafe] foldl vs foldl'

Daryoush Mehrtash dmehrtash at gmail.com
Wed Nov 5 13:01:12 EST 2008


Lets assume we don't have undefined in the list, are there functions (or
properties in the function) that would cause foldl to have different results
than foldl'?

daryoush

On Tue, Nov 4, 2008 at 3:37 PM, Daniel Fischer <daniel.is.fischer at web.de>wrote:

> Am Mittwoch, 5. November 2008 00:08 schrieb Daryoush Mehrtash:
> > Are there cases (function or list) where the result of foldl (or
> > foldr)would be different that foldl' (or foldr')?
> >
> > thanks,
> >
> > daryoush
>
> Simple example:
> import Data.List
>
> weird :: Int -> Int -> Int
> weird _ 0 = 0
> weird x y = x*y
>
> list :: [Int]
> list = [1, 2, 3, 4, undefined, 6, 7, 8, 9, 0]
>
> okey = foldl weird 1 list
>
> boom = foldl' weird 1 list
>
> *Main> okey
> 0
> *Main> boom
> *** Exception: Prelude.undefined
>
> since foldl' evaluates strictly (to WHNF), it can die on encountering an
> undefined value in the list where foldl doesn't.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20081105/239150b4/attachment.htm


More information about the Haskell-Cafe mailing list