1

I'm writing a simple Abstract Data Type for fractions and I cant even get the constructor to work, I'm just very lost on SML syntax.

signature fracs = sig

type fraction

exception zero_denominator

(* constructor *)
val // : int * int -> fraction

/*******************************************************************************************************************************************/

that is the .sig file showing the constructor that i am implementing. Here is what i have so far. the error i recieve is that I'm doing int*int* -> int when i want int*int-> fraction. I know what its saying and everything, but I just cant make it happen.

structure fracs :> fracs = struct

abstype fraction = frac of int * int
with

exception zero_denominator;
(**********************************)

(*constructor*)
infix 8 //;

fun num // den = if den = 0 then raise zero_denominator 

else
    num * den;

end;(*end ADT*)

end;(*end struct*)

1 Answer 1

2

In function signature, A * B means a tuple with two elements of types A and B. However in implementation, A * B means multiplication between two integers.

What you want is to make a fraction value from two integers:

fun num // den = 
    if den = 0 then raise zero_denominator else frac(num, den)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.