index
I found a few ways to look at folds around the internet, and jotted them down here to have them in one place.
foldr f z [x1, x2, ..., xn] = x1 `f` (x2 `f` ... (xn `f` z)...)
foldl f z [x1, x2, ..., xn] = (...((z `f` x1) `f` x2) `f`...) `f` xn
http://foldr.com/ 1+(…)=∞
http://foldl.com/ (…)+1=∞
Mnemonic: the associativity corresponds with where the initial element is tossed.
R -> 1 2 3 4 ... n z
L -> z 1 2 3 4 ... n
And any hopes for laziness happen away in foldr in left argument, away from buried initial element. i.e., initial element is buried.
foldr :: (evolv -> nextElem -> evolv) -> evolv -> [nextElem] -> evolv
foldl :: (nextElem -> evolv -> evolv) -> evolv -> [nextElem] -> evolv
Mnemonic: associativity corresponds with whether initial element is far left or far right, which corresponds with function taking initial/evolving value first or second).
images
foldl f z
foldr f z