Foldr code snippet

The function traverse takes an element and a map, and returns another element and a new (updated) map.

Given a list of elements xs0 and an initial map m0, I need to apply traverse to each element of the list while passing in the updated map to each new invocation of traverse, to produce a list of resulting elements and the last returned map:

foldr (\x (xs, m) -> let (x', m') = traverse x m
in (x':xs, m')) ([], m0) xs0


Is there a way to reduce all this clutter and use prelude as much as possible?

• Are you using the standard traverse from the Traversable class, or something else? If it's something else, what's its type? And for that matter, what's the type of the full expression? – Carl Apr 17 '17 at 6:06

You pretty much want mapAccumR, except that one has all of the argument orders and pair orders backwards, or the State monad, except that one passes its state to the right. We can define our own mapAccumR:
mapAccumR :: (a -> s -> (b, s)) -> [a] -> s -> ([b], s)

Now your code is mapAccumR traverse, except that you had the argument order wrong at the end there. :)
This implementation of mapAccumR is almost identical to your code, but the average reader will have heard of mapAccumR and the average stackoverflow voter evidently finds its code confusing out of context.