【发布时间】:2015-03-16 13:38:43
【问题描述】:
我有两种树:
type ref_tree = Node of int * ref_tree list ref
type tree = Node of int * tree list
我想编写一个函数convert: ref_tree -> tree,该函数将树与邻居保存为包含它们的列表的引用,并输出一棵树,其中引用更改为普通列表。这是我尝试过的:
let rec convert t =
match t with
| Node (x, l) ->
if (!l = []) then Node (x, []) else
Node (x, (List.map convert !l))
但是 OCaml 在尝试编译时返回错误:
if (!l = []) then Node (x, []) else
Error: This expression has type 'a list
but an expression was expected of type tree list ref
This expression 指向Node (x, []) 内的空列表。为什么会出现类型不匹配?
【问题讨论】: