Fold Left (foldl)
foldl :: (a -> b -> a) -> a -> [b] -> a
foldl f v [] = v
foldl f v (x:xs) = foldl f (f v x) xsfoldl (+) 0 [1, 2, 3]
foldl (+) (0 + 1) [2, 3]
foldl (+) ((0 + 1) + 2) [3]
foldl (+) (((0 + 1) + 2) + 3) []
(((0 + 1) + 2) + 3)lengthr :: [a] -> Int
lengthr = foldr (\_ n -> n + 1) 0 -- list element first, accumulator second
ghci> lengthr [1, 2, 3]
3lengthl :: [a] -> Int
lengthl = foldl (\n _ -> n + 1) 0 -- accumulator first, list element second
Prelude> lengthl [1, 2, 3]
3Last updated