是什么导致OCaml中出现此类型错误?
上面说 错误:此表达式的类型为expr,但表达式的类型应为int 我不知道为什么会不断出现错误是什么导致OCaml中出现此类型错误?,ocaml,Ocaml,上面说 错误:此表达式的类型为expr,但表达式的类型应为int 我不知道为什么会不断出现错误 type expr = NUM of int | PLUS of expr * expr | MINUS of expr * expr let rec calc expr1 = match expr1 with | NUM i -> NUM i | PLUS (lexpr1, rexpr1) ->
type expr = NUM of int
| PLUS of expr * expr
| MINUS of expr * expr
let rec calc expr1 =
match expr1 with
| NUM i -> NUM i
| PLUS (lexpr1, rexpr1) ->
(match lexpr1, rexpr1 with
| (*(NUM li1,NUM ri1) -> NUM li1+ri1*)
| (lexpr1', rexpr1') -> PLUS (calc lexpr1', calc rexpr1'))
NUM li1+ri1
首先创建一个NUM,然后尝试添加ri1。但由于+运算符仅适用于int,而不适用于expr和int,因此它会抛出一个错误
要首先修复此添加ints,请执行以下操作:
NUM (li1+ri1)