We can evaluate, say, myReverse [1,2,3] . We need a foldl definition
foldl fz [] = z foldl fz (x:xs) = foldl f (fzx) xs
So we have
myReverse [1,2,3,4]
with an important warning in [1] and for each stage of beta recovery, that this beta reduction actually occurs only when something checks the result. As foldl progresses foldl repeating f applications grow like thunks, so we really get (if f = \ax -> x:a ):
foldl f [] [1,2,3] foldl f (f [] 1) [2,3] foldl f ((f 2 (f [] 1))) [3] foldl f (((f 3 ((f 2 (f [] 1)))))) [] (((f 3 ((f 2 (f [] 1))))))
That's why we have a foldl' , which is strict in its battery and prevents it from building up.
The initial value is [] . [b] in this case matches a in foldl , which is [a] in myReverse .
source share