Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. data Values = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King | Ace deriving (Show)
  2.  
  3. data Suits = Spades | Hearts | Diamonds | Clubs deriving (Show)
  4.  
  5. type Cards = (Suits, Values)
  6. {- greaterCard
  7.  
  8. Compares the values of two playing cards and establishes which has the greatest value
  9. PRE: Values = [2, ..., 14]
  10. RETURNS: Card with the greatest value
  11. EXAMPLES: greaterCard (Spades Ace) (Spades King) = True
  12. greaterCard (Hearts King) (Spades King) = False
  13. -}
  14.  
  15. greaterCard :: Cards -> Cards -> Bool
  16. greaterCard (suits1, value1) (suits2, value2)
  17. | convertValue value1 > convertValue value2 = True
  18. | convertValue value1 == convertValue value2 = if convertSuits suits1 > convertSuits suits2 then True else False
  19. | otherwise = False
  20.  
  21. {- convertValue
  22.  
  23. -}
  24. convertValue :: Values -> Int
  25. convertValue Two = 2
  26. convertValue Three = 3
  27. convertValue Four = 4
  28. convertValue Five = 5
  29. convertValue Six = 6
  30. convertValue Seven = 7
  31. convertValue Eight = 8
  32. convertValue Nine = 9
  33. convertValue Ten = 10
  34. convertValue Jack = 11
  35. convertValue Queen = 12
  36. convertValue King = 13
  37. convertValue Ace = 14
  38.  
  39. {- convertSuits
  40.  
  41. -}
  42. convertSuits :: Suits -> Int
  43. convertSuits Spades = 4
  44. convertSuits Hearts = 3
  45. convertSuits Diamonds = 2
  46. convertSuits Clubs = 1
  47.  
  48. (+?) :: Maybe Int -> Maybe Int -> Maybe Int
  49. _ +? Nothing = Nothing
  50. Nothing +? _ = Nothing
  51. Just x +? Just y = Just (x+y)
  52.  
  53. (-?) :: Maybe Int -> Maybe Int -> Maybe Int
  54. _ -? Nothing = Nothing
  55. Nothing -? _ = Nothing
  56. Just x -? Just y = Just (x-y)
  57.  
  58. (*?) :: Maybe Int -> Maybe Int -> Maybe Int
  59. _ *? Nothing = Nothing
  60. Nothing *? _ = Nothing
  61. Just x *? Just y = Just (x * y)
  62.  
  63. (/?) :: Maybe Int -> Maybe Int -> Maybe Int
  64. _ /? Nothing = Nothing
  65. Nothing /? _ = Nothing
  66. Just x /? Just y
  67. | y == 0 = Nothing
  68. | otherwise Just (div x y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement