[Haskell-beginners] folds -- help!

John Dorsey haskell at colquitt.org
Tue Mar 10 16:54:22 EDT 2009


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



More information about the Beginners mailing list