小编hao*_*hao的帖子

使用foldl和没有样板的Reader monad递归地行走AST

我穿越了AST使用简单的模式匹配,并Reader单子.

在我的项目的其他地方,我已经定义了一个walk遍历AST 函数,函数的核心用于foldl将访问树中每个节点的结果减少为单个幺半群结果(例如,从特殊结果生成"符号表")树中的节点).

我的问题是:是否可以将这两种方法结合起来并使用像我的函数这样的walk函数:

walk :: Monoid a => (Node -> a) -> a -> Node -> a
walk f acc n = foldl (walk f) (acc <> f n) children
  where
    children = case n of
      Blockquote b           -> b
      DocBlock d             -> d
      FunctionDeclaration {} -> functionBody n
      List l                 -> l
      ListItem i             -> i …
Run Code Online (Sandbox Code Playgroud)

monads haskell parsec fold reader-monad

3
推荐指数
1
解决办法
389
查看次数

标签 统计

fold ×1

haskell ×1

monads ×1

parsec ×1

reader-monad ×1