Skip to content

Instantly share code, notes, and snippets.

@Elzair
Last active August 29, 2015 14:13
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 Elzair/6a5a2b9048bd2d22bd3d to your computer and use it in GitHub Desktop.
Save Elzair/6a5a2b9048bd2d22bd3d to your computer and use it in GitHub Desktop.
Haskell Notes

Functors

Definition

class Functor f where
  fmap :: (a -> b) -> f a -> fb

Laws

fmap id = id
fmap (f . g) F = fmap f (fmap g F) -- for any Functor F

Applicative Functors

Definition

class (Functor f) => Applicative f where
  pure  :: a -> f a
  (<*>) :: f (a -> b) -> f a -> f b
(<$>) :: (Functor f) => (a -> b) -> f a -> f b
f <$> x = fmap f x

Laws

pure f <*> x = fmap f x 
pure id <*> v = v
pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
pure f <*> pure x = pure (f x)
u <*> pure y = pure ($ y) <*> u

Monoids

Definition

class Monoid m where
  mempty :: m
  mappend :: m -> m -> m
  mconcat :: [m] -> m
  mconcat = foldr mappend mempty

Laws

mappend mempty x = x
mappend x mempty = x
mappend (mappend x y) z = mappend x (mappend y z)

Monads

Definition

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

Laws

Left Identity

return x >>= f

is equivalent to

f x

Right Identity

m >>= return

is equivalent to

m

Associativity

(m >>= f) >>= g

is equivalent to

m >>= (\x -> f x >>= g)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment