I'm playing with the functions from recursion-schemes
, and struggling to figure out if it offers something that generalized mapAccumR
. Something powerful enough to implement e.g.:
f :: [Int] -> (Int,[Int])
f [] = (0,[])
f (x:xs) = let (sz,xs') = f xs in (sz+x, x*sz : xs')
...in a single pass for a Fix
-ed structure, like:
data L a as = Empty | C a as
input :: Fix (L Int)
input = Fix (C 1 (Fix (C 2 (Fix Empty))))
zygo
seems to be nearly what I want, except that I need access to the final b
(the final sum in the example above).
My actual use case is type-checking an AST while annotating, and returning the type of the expression.
You want to descend upwards through
Fix f
tweaking values, while keeping track of a state as mapAccumR does? That'scataM
for the upward order, with theState
monad for the state to be kept track of. In your example:Using
lens
:All untested.
Or do you want the state to be local to each branch?