Created
June 17, 2014 02:05
-
-
Save psibi/8df3292dd3c884440c55 to your computer and use it in GitHub Desktop.
Binary tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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