GothLoli
GothLoli

Reputation: 19

SML, unbound variable or constructor

This is my code, and it keeps giving me unbound error message.

I'm actually really new to SML. so, i really dont know how to fix it.

It seems like using a and b is bad thing.

I tried to declare a and b

like this

a : int;
b : int;

but still doesn't work.

How can i fix this?

Upvotes: 0

Views: 833

Answers (2)

sshine
sshine

Reputation: 16105

Besides the missing |s between function clauses that Andreas mentions, you cannot apply the +, -, * and div operators to values of type calctree. You need to reduce each a and b to integers using your function first. For example,

datatype ops = PLUS | MINUS | TIMES | DIV
datatype calctree = LEAF of int | CALC of ops * calctree * calctree

fun getOp PLUS = op +
  | getOp MINUS = op -
  | getOp TIMES = op *
  | getOp DIV = op div

fun calc (LEAF x) = x
  | calc (CALC (oper, a, b)) = getOp oper (calc a, calc b)

Upvotes: 1

Andreas Rossberg
Andreas Rossberg

Reputation: 36088

It's just a syntax problem. All your code is missing is a | to separate the clauses of calculate. Just add one at the beginning of lines 5-8.

Upvotes: 1

Related Questions