MapAccumR-like recursion scheme over Fix?

I play with functions recursion-schemesand struggle to find out if it offers something generalized mapAccumR. Something powerful enough to implement, for example:

f :: [Int] -> (Int,[Int])
f [] = (0,[]) 
f (x:xs) = let (sz,xs') = f xs in (sz+x, x*sz : xs')

... in one pass for the Fix-ed structure , for example:

data L a as = Empty | C a as

input :: Fix (L Int)
input = Fix (C 1 (Fix (C 2 (Fix Empty))))

zygoIt seems almost what I want, except that I need access to the final one b(the final amount in the example above).

My actual use case is an AST type check on annotations and an expression type return.

+4
source share
1 answer

Fix f , , mapAccumR? cataM , State , . :

f :: Fix (L Int) -> (Fix (L Int), Int)
f = (`runState` 0) $ cataM $ ((.) . fmap) Fix $ \case
  Empty -> pure Empty
  C x xs -> do
    sz <- get
    put $ sz+x
    return $ C (x*sz) xs

lens:

makeClassyPrisms ''L

f = (`runState` 0) . transformMOf . _Fix . _C . _1 $ \x -> do
  sz <- id <<+= x
  return $ x*sz

.

, ?

0

Source: https://habr.com/ru/post/1664194/


All Articles