(.) :: (b -> c) -> (a -> b) -> a -> c |
function composition
|
($) :: (a -> b) -> a -> b |
application operator, has low, right-associative binding precedence, for example
f $ g $ h x = f (g (h x)) |
|
typeclass where empty/append are defined
|
|
mempty :: Monoid a => a |
identity of mappend (mappend mempty x = x)
|
mappend :: Monoid a => a -> a -> a |
append two monoids (associative: brackets does not matter)
mappend x (mappend y z) = mappend (mappend x y) z |
<> :: Monoid m => m -> m -> m |
infix synonym for mappend (
"he" <> "llo") |
mconcat :: [a] -> a |
fold list using mappend and mempty
|
|
typeclass where
fmap (map/<$>) is defined |
|
|
should satisfy laws
|
fmap id == idfmap (f . g) == fmap f . fmap g |
fmap :: Functor f => (a -> b) -> f a -> f b |
map function over functor
fmap (+1) (Just 3) is Just 4 |
<$> :: Functor f => (a -> b) -> f a -> f b |
function mapped over functor
infix synonym for fmap(+1) <$> (Just 3) is Just 4 |
|
typeclass where
pure/<*> are definedhave Functor as super class |
every instance of Applicative must have instance of Functorso fmap (map/<$>) can be used |
pure :: Applicative f => a -> f a |
create an instance of Applicative
pure 3 :: [Int] is [3]pure 3 :: Maybe Int is Just 3pure (+3) :: Maybe (Int -> Int) is Just a function from Int to Intpure (+3) :: [Int -> Int] is list of functionpure 1 :: IO Int is how it is printed in ghci |
(<*>) :: Applicative f => f (a -> b) -> f a -> f b |
sequential application / apply
Just (+1) <*> Just 1 :: Maybe Int is Just 2[(+1), (+2)] <*> [0] :: [Int] is [1, 2] |
|
typeclass
have Applicative as super class |
every instance of Monad must have instance of Applicative and Functorso fmap (map/<$>) and <*>/pure can be used |
return :: Monad m => a -> m a |
is
pure |
(>>) :: Monad m => m a -> m b -> m b |
sequentially compose two monads, first is usually
Just 2 >> Just 3 is Just 3Nothing >> Just 3 is Nothing[9, 9] >> [0, 0, 0] is [0,0,0,0,0,0] |
(>>=) :: Monad m => m a -> (a -> m b) -> m b |
bind, sequentially compose two monads, value of first passed as argument to the second
Just 3 >>= \x -> Just (x + 1) is Just 4Nothing >>= \x -> Just (x + 1) is Nothing[0, 0] >>= \x -> [x + 1] is [1, 1][0, 0] >>= \x -> [x + 1, 2] is [1,2,1,2][] >>= \x -> [x + 1] is [] |
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.