Created
August 13, 2017 02:49
-
-
Save exallium/d5a7b7cf674110c5e341b6d4bb48b2fe to your computer and use it in GitHub Desktop.
playing around with trees to solidify working with instance declarations
This file contains 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
data Tree a = Branch (Tree a) (Tree a) | Leaf a deriving Show | |
instance Functor Tree where | |
fmap fn (Branch l r) = Branch (fmap fn l) (fmap fn r) | |
fmap fn (Leaf a) = Leaf $ fn a | |
instance Applicative Tree where | |
(<*>) (Leaf fn) t = fmap fn t | |
(<*>) (Branch l r) t = Branch (l <*> t) (r <*> t) | |
pure = Leaf | |
instance Monad Tree where | |
(>>=) (Branch l r) fn = Branch (l >>= fn) (r >>= fn) | |
(>>=) (Leaf a) fn = fn a | |
return = Leaf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment