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.