ocaml的二叉参考树查找

ocaml的二叉参考树查找,ocaml,binary-tree,Ocaml,Binary Tree,我正在构建一个二元引用树,其中右叶节点查找左叶节点上的节点,如果所有右叶节点与一些左叶节点匹配,则返回true type exp = V of var | P of var * exp and var = string 但在这里,我一直在第5行遇到错误: let rec ctree : exp * exp -> bool =fun (e1,e2) -> match e2 with | P (x,y) -> match y with | P (

我正在构建一个二元引用树,其中右叶节点查找左叶节点上的节点,如果所有右叶节点与一些左叶节点匹配,则返回true

type exp = V of var
           | P of var * exp
and var = string
但在这里,我一直在第5行遇到错误:

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
这里的e1有一个exp类型,应该是这样的,但是编译器一直告诉我它应该是var=string类型。另外,对于6号线

| V a -> if a=x then true else ctree(e1,y)
它告诉我应该是var=string类型,而不是e1类型


有人能告诉我为什么会出错吗?

当有两个嵌套的
匹配表达式时,不清楚嵌套的结束位置。您需要在内部匹配周围使用括号。类似的方法可能会奏效:

V x -> e1=x
其次,函数的类型是
exp*exp->bool
,表示
e1
exp
类型。在函数末尾,您有以下内容:

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
由于
x
V
构造函数的值,因此它必须是字符串。但是只有当
e1
也是字符串时,
e1=x才有意义


因此,您在使用
e1

时存在类型冲突,谢谢您的回答Jeffrey!我对函数的类型有问题。它应该是var*exp->bool。与其他修复一起,一切正常。谢谢!
| V x -> e1 = x