Monads Flashcards
write small program to ask user’s name and say you rock.
main = do
putStrLn “Hello, what’s your name?”
name
how would you read: name
perform the I/O action getLine and then bind its result value to name. getLine
what is type signature for main?
main :: IO something, where something is some concrete type
what is the import statement for ‘when’?
import Control.Monad
What does mapM do? How does it differ from mapM_ ?
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.
what do monads do?
Monads apply a function that returns a wrapped value to a wrapped value. Monads have a function»_space;= (pronounced “bind”) to do this.
explain each section of the following type signature:
class Monad m where (>>=) :: m a -> (a -> m b) -> m b
(»=) m a = takes a monad
(a-> m b) = and a function which returns a monad
-> m b = and returns a monad.
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?
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»_space;= or liftM
What does the function liftM do?
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: