9. The countdown problem Flashcards

1
Q

What is a functor and how do you declare one?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two Functor laws

A

fmap id = id
fmap (g . h) = fmap g . fmap h

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are Applicatives and how do you declare one?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the four applicative laws?

A
  1. pure id <*> x = x
  2. pure (g x) = pure g <*> pure x
  3. x <> pure y = pure (\g -> g y) <> x
  4. x <> (y <> z) = (pure (.) <> x <> y) <*> z
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are monads and how do you declare one?

A

An applicative type m that supports return and&raquo_space;=

class Applicative m => Monad m where
return :: a -> m a
(»=) :: m a -> (a -> m b) -> m b
return = pure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly