Question

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*)
Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top