Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. module Haskell01 where
  2.  
  3. -- #TODO: put your macid in the following string
  4. macid = ""
  5.  
  6. {- ------------------------------------------------------------------------------------------------------------
  7. - Part 1: Either
  8. - ------------------------------------------------------------------------------------------------------------
  9. -}
  10. data MyEither a b = MyLeft a | MyRight b
  11. deriving Show
  12.  
  13. instance Functor (MyEither a) where
  14. fmap f (MyRight x) = MyRight $ f x
  15. fmap _ (MyLeft x) = MyLeft x
  16.  
  17.  
  18. instance Applicative (MyEither a) where
  19. pure val = MyRight val
  20. MyRight f <*> x = fmap f x
  21. MyLeft y <*> _ = MyLeft y
  22.  
  23. {- Task 1: (1 Mark)
  24. - Complete the following Monad Instance
  25. - Make sure it obeys the following laws
  26. - ------------------------------------------------------------------------------------------------------------
  27. - Left Identity: return a >>= f == f a
  28. - Right Identity: m >>= return == m
  29. - Associative: (m >>= f) >>= g == m >>= (\x -> f x >>= g)
  30. - ------------------------------------------------------------------------------------------------------------
  31. -}
  32. instance Monad (MyEither a) where
  33.  
  34.  
  35.  
  36. -- #TODO: Implement (>>=) :: MyEither a b -> (b -> MyEither a c) -> MyEither a c
  37. instance Monad (Either a) where
  38. return = Right
  39. Right b >>= f = f b
  40. Left a >>= _ = Left a
  41.  
  42. {- ------------------------------------------------------------------------------------------------------------
  43. - Part 2: List
  44. - ------------------------------------------------------------------------------------------------------------
  45. -}
  46. data List a = Cons a (List a) | Empty
  47. deriving Show
  48.  
  49. instance Functor List where
  50. fmap f (Cons x xs) = Cons (f x) (fmap f xs)
  51. fmap _ Empty = Empty
  52.  
  53. {- Task 2: (1 Mark)
  54. - Complete the following Monoid Instance
  55. - Make sure it obeys the following laws
  56. - ------------------------------------------------------------------------------------------------------------
  57. - Left Identity: mempty `mappend` x == x
  58. - Right Identity: x `mappend` mempty == x
  59. - Associative: (x `mappend` y) `mappend` z == x `mappend` (y `mappend` z)
  60. - ------------------------------------------------------------------------------------------------------------
  61. - Hint: mappend corresponds to the (++) function on built-in Haskell lists
  62. -}
  63.  
  64. -- #TODO: Implement mappend :: List a -> List a -> List a
  65.  
  66. instance Monoid (List a) where
  67. mempty = Empty
  68. (Cons x xs) mappend ys = Cons x (xs mappend ys)
  69. Empty mappend ys = ys
  70.  
  71.  
  72. {- Task 3: (1 Mark)
  73. - Complete the following Applicative Instance
  74. - Make sure it obeys the following laws
  75. - ------------------------------------------------------------------------------------------------------------
  76. - Identity: pure id <*> xs == xs
  77. - Homomorphism: pure f <*> pure x == pure (f x)
  78. - ------------------------------------------------------------------------------------------------------------
  79. - Hint: [f1,...fn] <*> [x1,...,xi] == f1 x1 `mappend` ... f1 xi `mappend` ... fn x1 `mappend` ... fn xi
  80. -}
  81.  
  82. -- #TODO: Implement <*> :: List (a -> b) -> List a -> List b
  83.  
  84. instance Applicative List where
  85. pure x = Cons x Empty
  86. (Cons f fs) <*> xs = fmap f xs `mappend` (fs <*> xs)
  87. Empty <*> _ = Empty
  88.  
  89.  
  90.  
  91. {- Task 4: (2 Marks)
  92. - Complete the following Monad Instance
  93. - Make sure it obeys the following laws
  94. - ------------------------------------------------------------------------------------------------------------
  95. - Left Identity: return a >>= f == f a
  96. - Right Identity: m >>= return == m
  97. - Associative: (m >>= f) >>= g == m >>= (\x -> f x >>= g)
  98. - ------------------------------------------------------------------------------------------------------------
  99. - Hint: [x1,..,xn] >>= f == f x1 `mappend` ... `mappend` f xn
  100. -}
  101. --instance Monad List where
  102. -- #TODO: Implement (>>=) :: List a -> (a -> List b) -> List b
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement