This implementation is incomplete because it does not handle fractional numbers. 1.1 type number = | Int of int | Float of float | Fraction
This implementation is incomplete because it does not handle fractional numbers.
1.1 type number =
| Int of int
| Float of float
| Fraction of int * int
The same lecture defined part of an implementation of the divide operation for these numbers:
let divide x y =
match (x, y) with
| (_, Int 0) | (_, Float 0.0) -> raise Division_by_zero
| (Int a, Int b) -> Int (a / b)
| (Float a, Float b) -> Float (a /. b)
| (Int a, Float b) -> Float (float a /. b)
| (Float a, Int b) -> Float (a /. float b)
Basic Operations
you write code in OCAML to implement the four basic arithmetic operations: add, subtract, multiply, and divide. You may adapt any of the above code in writing your own code.
Fractional numbers are challenging because they must be normalized after every operation. That is, if the result of an operation is 6/3, it should be converted to the integer 2, whereas if the result is 3 / 6, it should be converted to the fraction 1/2.
you must provide a normalize function which takes as parameters a numerator and a denominator and returns a number which is either an Int or a Fraction. In other words, the second case of the divide code above must be rewritten as
| (Int a, Int b) -> normalize a b so instead of truncating, the division is exact.
To normalize a fraction, and assuming neither the numerator nor the denominator is zero, you should compute the greatest common divisor (gcd) of the numerator and denominator. Dividing both the numerator and denominator by the gcd gives the normalized fraction or, if denominator = gcd, gives an integer.
You may use use either of the Euclidean Algorithms to compute the gcd. In its simplest form, this algorithm specifies:
- if the two numbers are the same, that number is the gcd
- otherwise, one number a is greater than the other number b, and the gcd is (recursively) the gcd of b and (a - b).
Your implementation of the gcd computation must use recursion, and this recursion must not use an accumulator. For this gcd function, it is fine to use if ... then ... else if ... then ... else ....
Either your gcd function must check that neither of the parameters is 0, or your normalize function must ensure that it never calls gcd with a 0 argument.
Number Conversions
In the example code in part 1.1, an integer result can only come by dividing one integer by another, and any operation between an integer and a float results in a floating point result.
With the addition of fractions,
- for every operation other than division, if the two numbers are of the same type, the result must also have the same type, except if both numbers are fractions and the result is a whole number, then the result will be an integer.
- any operation between a fraction and an integer may result in a fraction if the result is fractional, or an integer if the result is a whole number.
- any operation between an integer and a float results in a floating point result (same as in the divide function above).
- any operation between a fraction and a float results in a floating point result. Your code must first convert the fraction to an equivalent floating point number, then perform the operation between floats.
Modulo
Define a remainder or modulo function, which given two numbers, if they are both integers, returns the remainder of the division of the two integers.
Also define an appropriate OCAML exception. Your modulo function must raise this exception if either or both of the two arguments to the function are not integers. Your modulo function must use pattern matching to tell if the arguments are or are not integers, and also to test for the denominator being 0.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Heres an implementation of the basic arithmetic operations add subtract multiply and divide for the given number type in OCaml This implementation also includes a normalize function to handle fraction...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