(* initial version of the Rational structure that shows how we can group a type, its constructors, and some functions into a single unit. *) module Rational = struct type rational = Whole of int | Fraction of int * int let add(r1, r2) = match (r1, r2) with | (Whole i, Whole j) -> Whole(i + j) | (Whole i, Fraction(a, b)) -> Fraction(i * b + a, b) | (Fraction(a, b), Whole(i)) -> Fraction(i * b + a, b) | (Fraction(a1, b1), Fraction(a2, b2)) -> Fraction(a1 * b2 + a2 * b1, b1 * b2) let to_string(r) = match r with | Whole i -> string_of_int(i) | Fraction(a, b) -> string_of_int(a) ^ "/" ^ string_of_int(b) end