Skip to content

Instantly share code, notes, and snippets.

@exallium
Created August 13, 2017 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exallium/d5a7b7cf674110c5e341b6d4bb48b2fe to your computer and use it in GitHub Desktop.
Save exallium/d5a7b7cf674110c5e341b6d4bb48b2fe to your computer and use it in GitHub Desktop.
playing around with trees to solidify working with instance declarations
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