8.7
10 Monads
10.1 Overview
data Tree e = Empty | Branch e (Tree e) (Tree e) deriving (Show, Eq) instance Functor Tree where -- fmap :: (a -> b) -> Tree a -> Tree b fmap f Empty = Empty fmap f (Branch x l r) = Branch (f x) (fmap f l) (fmap f r) instance Monad Tree where -- return :: a -> Tree a return x = Branch x Empty Empty -- bind :: Tree a -> (a -> Tree b) -> Tree b Empty >>= k = Empty (Branch x l r) >>= k = let l' = l >>= k r' = r >>= k in case k x of Empty -> Empty Branch z _ _ -> Branch z l' r' -- Ignore this -- Avert your eyes instance Applicative Tree where pure x = Branch x Empty Empty (<*>) = undefined