forked from noelmarkham/learn-you-a-haskell-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-making-our-own-types-and-typeclasses.hs
49 lines (39 loc) · 2.08 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
40
41
42
43
44
45
46
47
48
49
import Data.List
{-
- 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 = Clubs | Diamonds | Hearts | Spades deriving (Show, Eq, Ord)
data Digit = Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Jack|Queen|King|Ace deriving (Show, Eq, Ord)
data Card = Card {digit :: Digit, suit :: Suit} deriving (Eq, Ord)
instance Show Card where
show (Card {digit = d, suit = s}) = "The " ++ show d ++ " of " ++ show s
-- We should be able to provide a function which returns the higher ranked card:
betterCard :: Card -> Card -> Card
betterCard x y = if x >= y then x else y
-- 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 = elem (Card Ace Spades) c
-- Create a new Coin type
data Coin = Heads | Tails deriving (Show, Eq)
-- 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 = elem True [elem Heads cgroup && length cgroup >= 10 | cgroup <- group c]
-- -- Have a play with implementing Hand for some other types, for instance Int and Bool
instance Hand Int where
play i = sum i == product i
instance Hand Bool where
play b = elem True b