|
You should print out your solutions to this assignment, and either turn them in at lecture, or leave them in the TA's mailbox at Sudikoff by class time on Mon. Nov. 16, 2009.
Please also e-mail an electronic version of your code to cs8hw@cs.dartmouth.edu, with the subject "CS 8 Short Assignment 10". Please attach your code as one or more enclosures to the message, and do not include it directly in the body of the e-mail message.
You must work alone on this short assignment.
Given the data type Rat for representing rational numbers:
data Rat = Rat {numer :: Integer, denom :: Integer}
make Rat an instance of Num and Ord by implementing the required functions within instance declarations. (Unlike what it shows on p. 161 of the book, you must define "<=" instead of "<" to make the default definitions for Ord work correctly.) Note that in order to do this you must also make Rat an instance of Eq and Show. The result of show on a Rat should be a string consisting of the numerator, a "/", and the denominator. Thus show (Rat 2 15) should produce the string "2/15".
Your code should create a function makeRat
makeRat :: Integer -> Integer -> Rat
that constructs
Rat data objects. You should never create Rat objects directly
(except in makeRat). This function should guarantee that:
Your other functions can then assume that these three facts are true about any Rat that they deal with. Any Rat that they create should be created via a call to makeRat.
For extra credit you may make Rat an instance of other numeric type classes.
Hand your module for Rat with all the required instance declarations. Include a test run demonstrating that all of the functions that you implemented work correctly.