lalala33rfs

Untitled

Nov 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. data Tree a = Nil | Branch (Tree a) a (Tree a) deriving (Eq, Show)
  2.  
  3. instance Foldable Tree where
  4. -- foldr :: (a -> b -> b) -> b -> Tree a -> b
  5. foldr f ini Nil = ini
  6. foldr f ini (Branch a b c) = foldr f (f b (foldr f ini c)) a
  7.  
  8. instance Functor Tree where
  9. fmap _ Nil = Nil
  10. fmap f (Branch l x r) = Branch (fmap f l) (f x) (fmap f r)
  11.  
  12. instance Applicative Tree where
  13. pure f = Branch (pure f) f (pure f)
  14. (<*>) Nil _ = Nil
  15. (<*>) _ Nil = Nil
  16. (<*>) (Branch fl fx fr) (Branch l x r) = Branch (fl <*> l) (fx x) (fr <*> r)
  17.  
  18. instance Traversable Tree where
  19. --traverse :: (a -> f b) -> t a -> f (t b)
  20. traverse _ Nil = pure Nil
  21. traverse g (Branch a b c) = Branch <$> (traverse g a) <*> g b <*> (traverse g c)
Add Comment
Please, Sign In to add comment