我正在尝试用https://hackage.haskell.org/package/hxt解析一个XML文件。在下面的代码中,我需要使用从外部标记中的属性获得的额外参数来解析内部元素。但是i在第二行不可用(第二个箭头?)。如何将i传递给getTerminal
getSentence = atTag "s" >>>
proc x -> do
i <- getAttrValue "id" -< x
ts <- listA (getTerminal i) <<< atTag "terminals" -< x
returnA -< Sentence { sid = i, terminals = ts }
...
getTerminal sid = atTag "t" >>>
proc x -> do
i <- getAttrValue "id" -< x
lem <- getAttrValue "lemma" -< x
returnA -< Terminal { lemma = lem, tid = nid i }
where
nid x = fromMaybe x (stripPrefix (sid ++ "_") x)也就是说,i包含我想要传递给getTerminal的值。
发布于 2021-03-23 11:57:11
使用-<<而不是-<使本地绑定对箭头可用:
getSentence = atTag "s" >>>
proc x -> do
i <- getAttrValue "id" -< x
ts <- listA (getTerminal i) <<< atTag "terminals" -<< x
returnA -< Sentence { sid = i, terminals = ts }请注意,-<<是根据ArrowApply的app实现的。例如,抛开语法上的笨拙不谈,上面的出现可以替换为:
ts <- app <<< listA . getTerminal *** atTag "terminals" -< (i, x)hxt的相关箭头是ArrowApply的实例,因此-<<可以与它们一起使用。
https://stackoverflow.com/questions/66756351
复制相似问题