[prev in list] [next in list] [prev in thread] [next in thread] 

List:       haskell-cafe
Subject:    Re: [Haskell-cafe] strictness properties of monoidal folds
From:       Alexey Khudyakov <alexey.skladnoy () gmail ! com>
Date:       2011-08-03 22:10:34
Message-ID: 4E39C75A.9080104 () gmail ! com
[Download RAW message or body]

On 02.08.2011 08:16, Sebastian Fischer wrote:
> Data.Foldable also provides the monoidal fold function foldMap. It is
> left unspecified whether the elements are accumulated leftwards,
> rightwards or in some other way, which is possible because the combining
> function is required to be associative. Does this additional freedom for
> implementors go so far as to allow for strict accumulation? Is it safe
> to implement foldMap (without prime) with a strict accumulator or are
> there examples where lazy accumulation is useful like in the above foldr
> example and distinguishable from strict accumulation without breaking
> the monoid laws?
>
Left and right folds behave identically for finite structures but they 
are different for infinite ones. Here is an example:

> island = map First $ Just "Snark" : repeat Nothing
>
> foldr mappend mempty island = First {getFirst = Just "Snark"}
> foldl mappend mempty island = ⊥

If mappend is lazy arguments then strict and lazy could be distingushed. 
And Last indeed offers an example:

 > island = [ error "Boojum", Last (Just "Snark") ]
 >
 > foldl  mappend mempty island = Last {getLast = Just "Snark"}
 > foldl' mappend mempty island = Last {getLast = *** Exception: Boojum


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic