简体   繁体   中英

Why does this give syntax error under TypeError?

let n1 = eval_expr e1 in
let n2 = eval_expr e2 in
(match n1, n2 with
 | Int_Val i, Int_Val j -> Int_Val i, Int_Val j
 | _ -> raise TypeError) in

let n3 = (n1 + n2) in
n3

It says syntax error under TypeError, why?

(If possible you should provide the full error message and show the associated position in the text.)

The error is actually located on the symbol in on the following line:

| _ -> raise TypeError) in 

In fact at that point there is no let for the in to be associated wtih. You have had two let s and two in s before that.

It seems to me that you're trying to extract the integer values i and j . You can do that something like this:

let i, j =
    match n1, n2 with
    | Int_val i, Int_val j -> i, j
    | _ -> raise TypeError
in
i + j

Your code applies + to n1 and n2 , which doesn't make sense since they appear to be values of some algebraic type (not int).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM