foldr/foldl/foldl'

Three variants of fold over lists. Note their strictness properties, and their demands on stack space (roughly corresponding to chains of yellow thunks).

import Observe

main = printO $ observe "foldr" foldr (+) 0 [1..4::Int]
import Observe

main = printO $ observe "foldl" foldl (+) 0 [1..4::Int]
import Observe

foldl' f a []     = a
foldl' f a (x:xs) = (foldl' f $! f a x) xs

main = printO $ observe "foldl'" foldl' (+) 0 [1..4::Int]