Guest User

Untitled

a guest
Jan 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Cards where
  2.  
  3. data Suit = Hearts
  4.           |Diamonds
  5.           |Spades
  6.           |Clubs
  7.     deriving (Eq, Enum)
  8.  
  9. dumbLookup :: (Eq a) => a -> [(a, b)] -> b
  10. dumbLookup _ [] = error "derp"
  11. dumbLookup z ((x,y):xs) | x == z = y
  12.                         | otherwise = dumbLookup z xs
  13.  
  14. instance Show Suit where
  15.     show x = dumbLookup x [(Hearts, "H"), (Diamonds, "D"), (Spades, "S"), (Clubs, "C")]
  16.    
  17. data Rank = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King | Ace
  18.     deriving (Eq, Ord, Enum)
  19.  
  20. instance Show Rank where
  21.     show x = -- see above, same trick but different values
  22. data Card = Card {rank :: Rank, suit :: Suit}
  23.  
  24. instance Show Card where show card = show (rank card) ++ show (suit card)
  25. instance Ord Card where compare card1 card2 = compare (rank card1) (rank card2)
  26. instance Eq Card where card1 == card2 = rank card1 == rank card2
Add Comment
Please, Sign In to add comment