type Fraction = (Integer, Integer)
ratplus :: Fraction -> Fraction -> Fraction
ratminus :: Fraction -> Fraction -> Fraction
rattimes :: Fraction -> Fraction -> Fraction
ratdiv :: Fraction -> Fraction -> Fraction
ratfloor :: Fraction -> Integer
ratfloat :: Fraction -> Float
rateq :: Fraction -> Fraction -> Bool
ratfloor will result in the greatest integer less than its argument.
An example of a hugs-dialogue:
Main> ratplus (3,5) (rattimes (2,3) (2,1))
(29, 15)
Main> ratminus (29,15) (3,1)
(-16, 15)
Main> ratfloor (-16,15)
-2
Main> ratfloat (15,8)
1.875
Main>
You can change the syntax to use infix notation for the binary
functions, and also make sure that the rational number is on canonical
form, i.e. the number 2/6 should be 1/3 etc.
We can now compute with exact rational numbers.
Implement an approximation of the square root of 2 by using the following iteration: The initial approximation of x is 1. The next approximation is obtained by letting the next value of x be (x + 2/x)/2. This last step is then repeated. You can do this by defining a function
iter :: Integer -> Fraction -> Fractionwhich iterates the same number of times as the value of the first argument.
:loadto read your definitions. If you change the file, you also have to reload it. Make sure that your hand-in has some examples how to run the programs.