# Functor, Applicative, Monoid, Monad # Functor Control.Applicative module ```haskell interface Functor ((->) r where fmap f g = (\x -> f (g x)) ``` ```haskell fmap :: (Functor f) => (a -> b) -> f a -> f b ``` # Applicative Functor Control.Applicative module ```haskell class (Funcvtor f) => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b ``` ```haskell (<gt;) :: (FUnctor f) => (a -> b) -> f a -> f b f <gt; x = fmap f x ``` Applicative Style ```haskell (++) <gt; Just "foo" <*> Just "bar" ``` # Monoid Data.Monoid module ```haskell class Monoid m where mempty :: m mappend :: m -> m -> m mconcat :: [m] -> m mconcat = foldr mappend mempty ``` List is monoid. ```haskell interface Monoid [a] where mempty = [] mappend = (++) ``` # Monad ```haskell class Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b x >> y = x >>= \_ -> y fail :: String -> m a fail msg = error msg ``` Maybe is monad. ```haskell interface Monad Maybe where return x = Just x Nothing >>= f = Notiong Just x >>= f = f x fail _ Nothing ```