9. The countdown problem Flashcards
What is a functor and how do you declare one?
The class of types that support mapping a function over all of it’s elements.
class Functor f where
fmap :: (a -> b) -> f a -> f b
What are the two Functor laws
fmap id = id
fmap (g . h) = fmap g . fmap h
What are Applicatives and how do you declare one?
A way to use functions with a variable amount of arguments
class Functor f => Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
What are the four applicative laws?
- pure id <*> x = x
- pure (g x) = pure g <*> pure x
- x <> pure y = pure (\g -> g y) <> x
- x <> (y <> z) = (pure (.) <> x <> y) <*> z
What are monads and how do you declare one?
An applicative type m that supports return and»_space;=
class Applicative m => Monad m where
return :: a -> m a
(»=) :: m a -> (a -> m b) -> m b
return = pure