Skip to content

Instantly share code, notes, and snippets.

@psibi
Created June 17, 2014 02:05
Show Gist options
  • Save psibi/8df3292dd3c884440c55 to your computer and use it in GitHub Desktop.
Save psibi/8df3292dd3c884440c55 to your computer and use it in GitHub Desktop.
Binary tree
import Control.Applicative
data Tree a = Leaf a
| Node (Tree a) (Tree a)
deriving Show
instance Functor Tree where
fmap f (Leaf a) = Leaf (f a)
fmap f (Node x y) = Node (fmap f x) (fmap f y)
instance Applicative Tree where
pure = Leaf
(Leaf f) <*> x = fmap f x
(Node x y) <*> a = Node (x <*> a) (y <*> a)
instance Monad Tree where
return = Leaf
(Leaf a) >>= f = f a
(Node x y) >>= f = Node (x >>= f) (y >>= f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment