Haskell was made by some really smart guys (with PhDs) (...) when a committee of researchers got together to design a kick-ass language.
- Miran Lipovaca "Learn You a Haskell for Great Good!: A Beginner's Guide" (No Starch Press, 2011)
A rational number is a number that can be expressed as a ratio n/d , where `n` and `d` are integers, except that `d` cannot be zero. `n` is called the numerator and `d` the denominator. Examples of rational numbers are 1/2 , 2/3 , 112/239 , and 2/1 . Compared to floating-point numbers, rational numbers have the advantage that fractions are represented exactly, without rounding or approximation.
The class we’ll design in this chapter must model the behavior of rational numbers, including allowing them to be added, subtracted, multiplied, and divided. To add two rationals, you must first obtain a common denominator, then add the two numerators. For example, to add 1/2 + 2/3 , you multiply both parts of the left operand by 3 and both parts of the right operand by 2, which gives you 3/6 + 4/6 . Adding the two numerators yields the result, 7/6 . To multiply two rational numbers, you can simply multiply their numerators and multiply their denominators. Thus, 1/2 ∗ 2/5 gives 2/10 , which can be represented more compactly in its "normalized" form as 1/5 . You divide by swapping the numerator and denominator of the right operand and then multiplying. For instance 1/2 / 3/5 is the same as 1/2 ∗ 5/3 , or 5/6 .