【发布时间】:2011-06-17 20:19:29
【问题描述】:
我正在尝试使用元组列表在 F# 中实现一棵树。[a] where a = (string, [a])
每个节点都有一个子节点列表,叶子节点是(name, [])
我希望能够像这样递归遍历列表的每一级。
a
b e
c d f g
然而,它们并不总是二叉树。
let t2 = [("a", [("b", [("c", []), ("d", [])]), ("e", [("f", []), ("g", [])])])]
let rec checkstuff tple =
match tple with
| (_, []) -> true
| (node, children) ->
List.fold ( || ) false (List.map checkstuff children)
我明白了:
类型不匹配。期待一个
('a * 'b list) list
但给了一个'b list
统一''a'和''b * 'a list'时生成的类型将是无限的
有没有办法我可以做这样的事情,或者不支持这样的元组递归列表?
【问题讨论】: