-
Notifications
You must be signed in to change notification settings - Fork 150
/
08-making-our-own-types-and-typeclasses.hs
39 lines (32 loc) · 1.54 KB
/
08-making-our-own-types-and-typeclasses.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{-
- We are going to create some types for a deck of cards
- The cards need to have an ordering, based on the standard ranking http://en.wikipedia.org/wiki/Standard_52-card_deck#Rank_and_color
- We are assuming Aces are high.
- Therefore, the following statements should be true:
- (Card Ace Spades) > (Card King Spades)
- (Card Two Clubs) < (Card Three Clubs)
-
- We are going to provide our own implementation of the Show typeclass for the Card type.
- When displaying the Card instance in GHCI, or calling show (Card digit suit), the String which should be displayed is "The <Digit> of <Suit>"
-
- Uncomment the following declarations to complete the implementation, and provide an implementation for instance Show Card
-}
--data Suit =
--data Digit =
--data Card =
-- We should be able to provide a function which returns the higher ranked card:
betterCard :: Card -> Card -> Card
betterCard x y = undefined
-- Here is a new Typeclass, which represents some kind of playing hand in a game.
-- It returns True for a "winning hand", depending on the rules for the type of class we are playing with
class Hand a where
play :: [a] -> Bool
-- Implement Hand for Card, where play returns true if the list contains the Ace of Spades
instance Hand Card where
play c = undefined
-- Create a new Coin type
--data Coin =
-- Implement Hand for Coin, where play returns true if there are ten heads in a row in the list
instance Hand Coin where
play c = undefined
-- Have a play with implementing Hand for some other types, for instance Int and Bool