steshaw (13) [Avatar] Offline
#1
The book has the calculation via foldl as:

In our first example, foldr would calculate the result as 1 + (2 + (3 + (4 + 0))) whereas foldl would calculate the result as (((1 + 2) + 3) + 4) + 0

Whereas is should be: (((0 + 1) + 2 ) + 3 ) + 4.

I was able to verify this using the Expr module:

*Expr> the (Expr _) (foldr (+) 0 [1,2,3,4])
Add (Val 1) (Add (Val 2) (Add (Val 3) (Add (Val 4) (Val 0)))) : Expr Integer
*Expr> show $ the (Expr _) (foldr (+) 0 [1,2,3,4])
"(1 + (2 + (3 + (4 + 0))))" : String
*Expr> show $ the (Expr _) (foldl (+) 0 [1,2,3,4])
"((((0 + 1) + 2) + 3) + 4)" : String
Edwin Brady (65) [Avatar] Offline
#2
So it should. Thank you!