12. Monads and more Flashcards

1
Q

What is a Functor used for?

A

For mapping a function over each element in a class

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

What is the declaration of the Functor class?

A

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
3
Q

Describe the functor definition of the Maybe class

A

instance Functor Maybe where
fmap _ Nothing = Nothing
fmap f Just x = Just (f x)

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

What is an Applicative used for?

A

As an extension of the Functor class so that it can take multiple arguments.

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

What is the decleration of the Applicative class?

A

class Functor => 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
6
Q

What is a Monad used for?

A

To abstract away details of computations that involve side-effects or other complexities.

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

What is the definition of the Monad class?

A

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

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

Describe the bind function “»=”

A

Takes a value of the monad type and a function that takes a plain value and returns a monad value, and applies the function to the plain value.

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

Describe the monad ‘return’ function

A

Also called pure, takes a plain value and wraps it in the monad type.

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

Describe the Monad laws

A
  1. return x&raquo_space;= f = f x
  2. mx&raquo_space;= return = mx
  3. (mx&raquo_space;= f)&raquo_space;= g = mx&raquo_space;= (\x -> (f x&raquo_space;= g))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly