OCaml括号语法错误:括号不匹配

OCaml括号语法错误:括号不匹配,ocaml,Ocaml,我编写了一个函数,对二叉树中的所有元素求和: type 'a tree = Leaf of 'a | Node of 'a tree * 'a * 'a tree;; let rec sum_tree tree = match tree with | Leaf l -> l | a -> a | Node(tree t1, a, tree t2) -> sum_tree t1 + sum_tree a + sum_tree t2;; 然而,当我试图编译这段代码时,我遇到了一

我编写了一个函数,对二叉树中的所有元素求和:

type 'a tree = Leaf of 'a | Node of 'a tree * 'a * 'a tree;;

let rec sum_tree tree = match tree with
| Leaf l -> l
| a -> a
| Node(tree t1, a, tree t2) -> sum_tree t1 + sum_tree a + sum_tree t2;;
然而,当我试图编译这段代码时,我遇到了一个语法错误:

let rec sum_tree tree = match tree with
| Leaf l -> l
| a -> a
| Node**(**tree **t1**, a, tree t2) -> sum_tree t1 + sum_tree a + sum_tree t2;;

Syntax error: ')' expected, the highlighted '(' might be unmatched

此代码有什么问题?

您有这样一种模式:

Node(tree t1, a, tree t2)
但是没有形式为
ab
的模式,所以
树t1
作为子模式没有任何意义。不清楚你想用这个模式做什么。无论如何,这就是导致语法错误的原因

很可能您只想写以下内容:

Node (t1, a, t2)

现在有3个子模式将与节点的三个子部分相匹配。

有这样一个模式:

Node(tree t1, a, tree t2)
但是没有形式为
ab
的模式,所以
树t1
作为子模式没有任何意义。不清楚你想用这个模式做什么。无论如何,这就是导致语法错误的原因

很可能您只想写以下内容:

Node (t1, a, t2)
现在有3个子模式将与节点的三个子部分相匹配。

代码块中不支持粗体吗?我不知道。。。对不起,这是上下文。括号和带双星的t1是突出显示的部分。代码块中是否不支持粗体?我不知道。。。对不起,这是上下文。括号和带双星的t1是突出显示的部分。