Foldl usage

foldl (+) 0 [1,2,3] == foldl (+) 0 1:2:3:[] == foldl (+) (((0+1)+2)+3) [] == ((( 0 + 1) + 2) + 3)

because lazy evaluation, the final expression will not be evaluated to 6 until its value is demanded.
GHC will use an interanal stack to store thunk. It can cause space leaks.

ex:
foldl (+) 0 [1..1000000]
*** Exception: stack overflow

Use foldl' or foldr instead!

fold ' _ zero [] = zero
fold' step zero (x:xs) = 
    let new = step zero zero x
    in new `seq` foldl' step new xs

function seq will force the first argument to be evaluated, and then return it second argument!

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License