prelude-ls
prelude-ls copied to clipboard
mapAccumL and mapAccumR
I suggest to add the functions:
mapAccumL :: (a -> b -> (a, c)) -> a -> [b] -> (a, [c])
mapAccumR :: (a -> b -> (a, c)) -> a -> [b] -> (a, [c])
from haskell Data.List
They afford to map a list with auxiliary value, kind of hybrid of map
and fold
, applies a function to each element of a structure, passing an accumulating parameter from left to right or vice versa, and returning a final value of this accumulator together with the new structure.
Possible implementation:
mapAccumL = (next, start, bs) -->
| empty bs => [start, []]
| otherwise =>
scanned = tail <| scanl (([a, c], b) -> next a, b), [start, null] <| bs
[head <| last scanned; map last <| scanned]