[Haskell-cafe] Re: [Haskell-beginners] folds -- help!

Adrian Neumann aneumann at inf.fu-berlin.de
Sat Mar 14 02:55:35 EDT 2009


That's why I said "for appropriate g and f". But I see that my  
wording was misleading.
However you can say:

 > foldl' oplus alpha bs =(foldr f id bs) alpha where
 >       f a g = \alpha -> g (alpha `oplus` a)
 >
 > foldr' oplus alpha bs = (foldl f id bs) alpha where
 >    f g a = \alpha -> g (a `oplus` alpha)

And it works as long as oplus is strict in both arguments.

Am 10.03.2009 um 21:54 schrieb John Dorsey:

> Adrian Neumann wrote:
>
>> Notice that there is no difference between
>>
>> foldr g a
>> foldl f a
>>
>> (for appropriate g and f) if g and f are strict in both arguments.
>
> Be careful... as apfelmus noted elsewhere in this thread, that's  
> not (in
> general) true.
>
> Prelude> foldr (^) 2 [3,5]
> 847288609443
> Prelude> foldl (^) 2 [3,5]
> 32768
>
> The reason?  Integer exponentiation (^) isn't associative and
> commutative.  So the first is (3 ^ (5^2)) = 3^25, while the second is
> ((2 ^ 3) ^ 5) = 2^15.
>
> Cheers,
> John
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 194 bytes
Desc: Signierter Teil der Nachricht
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20090314/8bae8007/PGP.bin


More information about the Haskell-Cafe mailing list