⏮ Más apuntes

FP summary

Elias Hernandis - 21 Jan 2020

Property based testing (QuickTest)

Lazy evaluation

WHNF (= Weak Head Normal Form)

An expression is in WHNF if any of the following are true:

  1. The expression is a constructor;
    • If the expression is a constructor that is being pattern matched on (e.g. a constructor inside a case ... of, then it will be reduced depending on the value of the constructor.
  2. The expression is an anonymous function, i.e. a lambda expression;
    • If lambda is being applied to all of its arguments, then it is reduced depending on their value.
  3. The expression is a function applied to too few arguments.

Higher-kinded abstractions

Functor

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

Functor laws

fmap id = id`
fmap (f . g) = fmap f . fmap g`

Aplicative

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

Monoid

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

Monoid laws

-- mempty is the identity element for <>
mempty <> x = x
x <> mempty = x

-- The <> operator is left and right associative
(x <> y) <> z = x <> (y <> z)

Monad

class Functor m => Monad m where
  (>>=)  :: m a -> (a -> m b) -> m b
  return :: a -> m a

Do notation

Aplicative

Traversable