我正在使用以下类型的树版本:
Tree = Empty | Leaf Event | Split String [(String, Tree)]
我的目标是获得一个函数,该函数返回一个对列表,[(Event,[Int])]
其中[Int]
包含每个事件的坐标(在树中到达它的路径),即如果树是:
Split str [(str2, Leaf event), (str3, Empty)]
然后我希望它返回[event,[0]]
。我想忽略树的任何空头。
所以我的功能看起来像
coords :: Tree -> [(Event,[Int])]
coords Empty = []
coords (Leaf event) = [event, []]
然后对于拆分它需要递归地在每个子树上应用该函数。我想过这样做:
coords Split str xs = zip (coords trees) [1..] where
trees = [snd(str,tree) | (str,tree) <-xs]
但这会给我提供任意长度的嵌套列表,以及其他几个问题。有任何想法吗?