我想制作一个函数标准 ml,它接受一个列表和函数,并从中制作一个 BST。该函数的类型是:'a list -> ('a * 'a -> bool) -> 'a tree
,但是我遇到了一些问题,这是我编写的代码:
datatype 'data tree =
EMPTY
| NODE of 'data tree * 'data * "data tree;
fun makeBST [] f = EMPTY
| makeBST (x::xs) f =
let
fun insert EMPTY x = NODE(EMPTY, x, EMPTY)
| insert (NODE(left, root, right)) x =
if f(x, root) then
insert left x
else
insert right x
in
makeBST xs f
end;
我使用此函数获得的类型是:'a list -> ('b * 'c -> bool) -> 'd tree
当我尝试调用它时,如下所示,makeBST [4, 3, 6, 7, 8, 2, 0, 1] (op <);
我收到以下错误:
stdIn:16.1-16.40 Warning: type vars not generalized because of
value restriction are instantiated to dummy types (X1,X2,...)
val it = EMPTY : ?.X1 tree
代码有什么问题?谢谢
编辑:
我的代码的第二个版本:
fun makeBST [] f = EMPTY
| makeBST (x::xs) f =
let
val tree = EMPTY
fun insert EMPTY x = NODE (EMPTY, x, EMPTY)
| insert (NODE(left, root, right)) x =
if f(x, root) then
insert left x
else
insert right x
in
insert (makeBST xs f) x
end;
这段代码产生了我想要的类型,但它正确吗?