mapAccumR
mapAccumR is a higher-order function in Haskell's Data.List module that maps a function over a list while threading an accumulator from right to left. It returns a pair consisting of the final accumulator and the list of mapped results. The function and the accumulator are both updated as the mapping proceeds from the end of the list toward the front, but the resulting list is produced in the original left-to-right order.
The type signature is mapAccumR :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y]). The first argument
mapAccumR (\acc x -> let acc' = acc + x in (acc', acc')) 0 [1,2,3]
This demonstrates the right-to-left threading of the accumulator: the first output element corresponds to the total
mapAccumR is the right-to-left counterpart of mapAccumL, which threads the accumulator from left to right. Both