DEV_idiot
DEV_idiot

Reputation: 1

What is causing this type error in OCaml?

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'))

It says

Error: This expression has type expr but an expression was expected of type int

I don't know why errors keep coming out

Upvotes: 0

Views: 58

Answers (1)

user934691
user934691

Reputation:

Ocaml in the line

NUM li1+ri1

first creates a NUM and then tries to add ri1. But since + operator applies only on ints and not at expr and int, it throws an error.

To fix this add ints first:

NUM (li1+ri1)

Upvotes: 0

Related Questions