我想制作一个函数标准ml,它需要一个列表和函数,并使BST不在列表中。该函数的类型是:'a list -> ('a * 'a -> bool) -> 'a tree
,但我有一些问题吧,下面是我写的代码:标准ml使bst不在列表中
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;
此代码生成我想要的类型,但它是正确的?
当然,这是从韦伯的现代编程语言第11章课本作业问题,锻炼11 – 2017-10-13 12:51:18