type exp = V of var
| P of var * exp
and var = string我正在构建一个二进制参考树,其中右叶节点查找左叶节点上的叶节点,如果所有右叶节点与左叶节点匹配,则返回true。
let rec ctree : exp * exp -> bool
=fun (e1,e2) -> match e2 with
| P (x,y) -> match y with
| P (a,b) -> if (ctree(a,b)) then true else ctree(x,b)
| V a -> if a=x then true else ctree(e1,y)
| V x -> e1=x但是在这里,我经常在第5行出现错误:
| V a -> if a=x then true else ctree(e1,y)这里的e1有一个类型exp,应该是这样的,但是编译器一直告诉我它应该是var=string类型。另外,对于第6行,
V x -> e1=x它告诉我,应该再次使用var=string类型而不是e1类型。
有人能告诉我为什么会出错吗?
发布于 2015-10-23 05:14:56
当您有两个嵌套的match表达式时,不清楚嵌套的结束位置。您需要在内部匹配周围使用括号。像这样的东西可能会起作用:
let rec ctree : exp * exp -> bool =
fun (e1,e2) -> match e2 with
| P (x,y) ->
(match y with
| P (a,b) -> if (ctree(a,b)) then true else ctree(x,b)
| V a -> if a=x then true else ctree(e1,y)
)
| V x -> e1=x其次,函数的类型是exp * exp -> bool,它表示e1是exp类型。在函数的末尾,您可以看到以下内容:
| V x -> e1 = x因为x是V构造函数的值,所以它必须是字符串。但是,只有当e1 = x也是字符串时,e1才有意义。
因此,在使用e1时存在类型冲突。
https://stackoverflow.com/questions/33295144
复制相似问题