Guest User

Untitled

a guest
Dec 11th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. import Control.Monad
  2.  
  3. data List a = Nil
  4. | Cons a (List a)
  5. deriving (Show, Eq)
  6.  
  7. instance Monoid (List a) where
  8. mempty = Nil
  9.  
  10. mappend xs Nil = xs
  11. mappend Nil ys = ys
  12. mappend xs ys = let go Nil l = l
  13. go (Cons x xs) l = go xs (Cons x l)
  14. in go (reverse' xs) ys
  15.  
  16. reverse' :: List a -> List a
  17. reverse' xs = let go Nil l = l
  18. go (Cons x xs) l = go xs (Cons x l)
  19. in go xs Nil
  20.  
  21. instance Functor List where
  22. fmap _ Nil = Nil
  23. fmap f (Cons x xs) = Cons (f x) (fmap f xs)
  24.  
  25. instance Applicative List where
  26. pure x = Cons x Nil
  27.  
  28. Nil <*> _ = Nil
  29. _ <*> Nil = Nil
  30. Cons f fs <*> xs = mappend (fmap f xs) (fs <*> xs)
  31.  
  32. instance Monad List where
  33. Nil >>= f = Nil
  34. Cons x xs >>= f = mappend (f x) (xs >>= f)
Add Comment
Please, Sign In to add comment