The
mapAccumL function behaves like a combination of
fmap and
foldl; it applies a function to each element of
a structure, passing an accumulating parameter from left to right, and
returning a final value of this accumulator together with the new
structure.
Examples
Basic usage:
>>> mapAccumL (\a b -> (a + b, a)) 0 [1..10]
(55,[0,1,3,6,10,15,21,28,36,45])
>>> mapAccumL (\a b -> (a <> show b, a)) "0" [1..5]
("012345",["0","01","012","0123","01234"])