Monads Flashcards

1
Q

write small program to ask user’s name and say you rock.

A

main = do
putStrLn “Hello, what’s your name?”
name

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

how would you read: name

A

perform the I/O action getLine and then bind its result value to name. getLine

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

what is type signature for main?

A

main :: IO something, where something is some concrete type

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

what is the import statement for ‘when’?

A

import Control.Monad

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

What does mapM do? How does it differ from mapM_ ?

A

mapM takes a function and a list, maps the function over the list and then sequences it. mapM_ does the same, only it throws away the result later. We usually use mapM_ when we don’t care what result our sequenced I/O actions have.

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

what do monads do?

A

Monads apply a function that returns a wrapped value to a wrapped value. Monads have a function&raquo_space;= (pronounced “bind”) to do this.

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

explain each section of the following type signature:

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

(»=) m a = takes a monad
(a-> m b) = and a function which returns a monad
-> m b = and returns a monad.

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

A functor is a data type that implements the Functor typeclass.
An applicative is a data type that implements the Applicative typeclass.
A monad is a data type that implements the Monad typeclass.
A Maybe implements all three, so it is a functor, an applicative, and a monad.
What is the difference between the three?

A

functors: you apply a function to a wrapped value using fmap or
applicatives: you apply a wrapped function to a wrapped value using or liftA
monads: you apply a function that returns a wrapped value, to a wrapped value using&raquo_space;= or liftM

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

What does the function liftM do?

A

Function liftM turns a function which takes input and produces output to a function which takes input in some monad and produces output in the same monad. Lets look at its definition:

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