Question
Ocaml: module type RATIONAL = sig (** The abstract type of rational numbers. *) type t (** [zero] is 0 (or: from_int 0). *) val
Ocaml:
module type RATIONAL = sig (** The abstract type of rational numbers. *) type t (** [zero] is 0 (or: from_int 0). *) val zero : t (** Document it *) val one : t (** [equal x y] tests whether [x] is equal to [y]. *) val equal : t -> t -> bool (** Document it *) val less_than : t -> t -> bool (** Document it *) val neg : t -> t (** [add x y] is the addition [x + y]. *) val add : t -> t -> t (** Document it *) val sub : t -> t -> t (** Document it *) val mul : t -> t -> t (** [div x y] is the division [x / y]. @raise Division_by_zero when if [y] is zero. *) val div : t -> t -> t (** Convert an abstract rational number to a pair of integers [(x, y)] such that 1. the rational number is equal to [x/y]; and 2. [x/y] is reduced and [y] is positive. For example, [(-4)/6] and [2/(-3)] should all output [(-2, 3)]. *) val to_pair : t -> int * int (** Convert a pair of integers [(x, y)] into an abstract rational number [x/y]. @raise Division_by_zero when if [y] is 0. *) val from_pair : int * int -> t end module Rational : RATIONAL = struct (** Fill out the content here. You can choose whatever type [t] to implement your rational numbers. You can also decide when to reduce rational numbers as long as [to_pair] outputs correct pairs. For this , please use the built-in exception [Division_by_zero] instead of defining your own [Division_by_zero].
Hint: For computing greatest common divisors, recall Euclid's algorithm: let rec gcd a b = if b = 0 then a else gcd b (a mod b);; Hint2: You'll need to do a bit more work to handle negative numbers. *) end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started