Haskell
We will now implement equivalent functionality in Haskell by declared a new algebraic type, Num with different instances of the Show, Eq, and Ord type classes.
Recommended reading:
Making Our Own Types and Typeclasses in Learn You a Haskell for Great Good!
Type Classes and Overloading (until recursive types) in A Gentle Introduction to Haskell
Consider this Haskell definition, which you should execute:
data MyNum = Fraction {numerator :: Integer, denominator :: Integer}
| Mixed {whole :: Integer, numerator :: Integer, denominator :: Integer}
deriving (Eq, Show)
Q. Provide a transcript showing the creation of fraction3/2and mixed number 1(1/2). Make sure you show how they are printed.
To improve the output, lets replace the derived default instance of Show. Modify the definition of Num to not derive a default instance of Show, and add this code:
instance Show MyNum where
show (Fraction n d) = (show n) ++ / ++ (show d)
show (Mixed w n d) = (show w) ++ ++ (show n) ++ / ++ (show d)
Q. Provide a transcript showing the new printed representations of 3/2 and 1 1/2.
Q. The implementation of show calls show. Why doesnt this cause an infinite loop?
Q. Provide a transcript in which you create and compare each of the following pairs:
1/2and 2//4
3/2and 1(1/2)
0(3/2)and 1(1/2)
All are equal, according to the rules of mathematics. Explain why the interpreter does not recognize them as equal. Provide examples of pairs that are considered equal.
Create an implementation of Eq that properly compares instances of our new type. You will probably want to create a helper method, like surface in Learn You a Haskell. Hint: Use fromIntegral to convert from integer to real-fractional types.
Q. Turn in your new and changed code. Provide a transcript showing equal and unequal examples of each of the four combinations of fraction and mixed. (You should turn in at least 8 comparisons.)
Create an implementation of the Ord typeclass for our new type.
Q. Turn in your new and changed code. Provide a transcript of your tests, which should show that less than, equals, and greater than all work for various combinations.
Note that it is not possible to make equality comparisons work with other types, since the type of eq is a -> a -> Bool.
Grading guide
Q. Provide a transcript showing the creation of fraction3/2and mixed number 1(1/2). Make sure you show how they are printed.
.5 for command to create 3/2
.5 for display of 3/2
.5 for command to create 1 1/2
.5 for display of 1 1/2
Q. Provide a transcript showing the new printed representations of 3/2 and 1 1/2.
.5 for command to create 3/2
.5 for display of 3/2
.5 for command to create 1 1/2
.5 for display of 1 1/2
Q. The implementation of show calls show. Why doesnt this cause an infinite loop?
2 for understanding the key idea and stating it clearly
Q. Provide a transcript in which you create and compare each of the following pairs
All are equal, according to the rules of mathematics. Explain why the interpreter does not recognize them as equal. Provide examples of pairs that are considered equal.
3: 1 for each of the 3 requested pairs (.5 for commands creating them, .5 for result)
2 for why interpreter does not judge them equal
1 for first example of equal pairs
1 for second example of equal pairs
Q. Turn in your new and changed code. Provide a transcript showing equal and unequal examples of each of the four combinations of fraction and mixed.
Code: 8
1 for not deriving the default instance
2 for correctly computing the value of a fraction
2 for correctly computing the value of a mixed
2 for correct comparisons
1 for good style
Transcript: 4 (.5 for each comparison)
Q. Turn in your new and changed code [for Ord]. Provide a transcript of your tests, which should show that less than, equals, and greater than all work for various combinations.
Code: 6
1 for not deriving default instance
1 for correctly ordering fraction & fraction
1 for correctly ordering fraction & mixed
1 for correctly ordering mixed & fraction
1 for correctly ordering mixed & mixed
1 for style
Transcript: 7
1 for having a test that returns LT
1 for having a test that returns EQ
1 for having a test that returns GT
1 for having a test of fraction & fraction
1 for having a test of fraction & mixed
1 for having a test of mixed & fraction
1 for having a test of mixed & mixed
Deductions
1 or more if tests do not fully cover code
/docProps/thumbnail.jpeg
Reviews
There are no reviews yet.