Monads let functional programmers chain computations with effects like failure, state or I/O while keeping code pure and composable.
In Haskell, what is the type of the bind operator (>>=)?
(a -> b) -> m a -> m b
m a -> (a -> m b) -> m b
m (a -> b) -> m a -> m b
m a -> (a -> b) -> m b only
Right! Bind takes a wrapped value and a function returning a new wrapped value, and chains them into m b.
Bind is m a -> (a -> m b) -> m b. The first option is fmap, the third is Applicative's <*>, and a -> m a is return.
What does `return 5` do inside a Haskell do-block?
Stops the do-block early, skipping later lines
Raises an error unless 5 is the last line
Runs 5 as a side effect in the IO world
Wraps 5 in the monad without ending the block
Exactly! return is just pure: it lifts a value into the monad, and the lines after it still run.
return only wraps 5 in the monad; it doesn't exit anything. Lines after it still run, unlike return in C or Python.
What is `Nothing >>= f` in the Maybe monad?
An exception is thrown at runtime
f applied to a default value
Nothing, and f is never called
Just Nothing, a nested box
Correct! Maybe short-circuits: once a step gives Nothing, the rest of the chain is skipped.
The result is Nothing, and f is never called. Maybe models failure as a plain value, with no exceptions.
Scratch the card to reveal a fact about monads
Eugenio Moggi brought monads from category theory into programming in 1989; Philip Wadler then made them popular in Haskell.
What does `[1,2] >>= \x -> [x, x*10]` give?
[1,10,2,20]
[1,10]
[[1,10],[2,20]]
[10,20]
Yes! The List monad applies the function to every element and flattens the results.
The answer is [1,10,2,20]. List bind maps over every element and concatenates; [[1,10],[2,20]] would be fmap without flattening.
Which law says `return a >>= f` equals `f a`?
Associativity
Right identity
Left identity
Functor composition
Right! Left identity: wrapping a value and binding it into f is the same as calling f directly.
It's left identity. Right identity is m >>= return = m, and associativity concerns how nested binds group.
Flip the card to reveal a fact about monads
Haskell 1.0 had no monadic IO; early programs handled input and output with lazy streams of requests and responses.
Did you know?
Does the Haskell compiler verify that your Monad instance obeys the monad laws?
Yes, the type checker rejects instances that break them
Yes, but only with optimisations switched on
No, the programmer must make sure they hold
No, because the laws only apply to the IO monad
Correct! The laws are a contract the compiler can't check; programmers prove or test them, e.g. with QuickCheck.
No: the compiler only checks types, so the programmer must make sure the laws hold. Code relies on them anyway, for example when refactoring do-blocks.
How does the State monad keep track of state without mutation?
It stores the state in a hidden global variable
It runs each step in the IO monad underneath
It reassigns do-block variables like a loop
It threads the state through functions s -> (a, s)
Exactly! State s a wraps a function s -> (a, s), and bind passes each new state on to the next step.
State is a function s -> (a, s), and bind threads the state from step to step. Nothing is mutated; it is pure function plumbing.
Why do Haskellers use monad transformers like StateT over IO?
Any two monads already combine directly into one
To make IO code pure by removing its effects
Applicative already stacks effects better
Monads don't compose in general, so stacks are built
Right! Functors and applicatives compose freely, but monads don't, so transformers layer one effect on another.
Monads don't compose in general, so transformers like StateT s IO build a stack that combines state with I/O.
Drag the slider to reveal a fact about monads
Ever written a monad without knowing it?
C# LINQ query syntax is monadic: SelectMany plays the role of bind, so LINQ works with lists, tasks and more.
What does Monad allow that Applicative alone does not?
Combining independent effects in parallel
Running side effects at all
Mapping a plain function over a value
Choosing the next effect based on an earlier result
Yes! With bind, the next computation can depend on the value produced before, which Applicative cannot express.
Monad lets the next effect depend on an earlier result. Applicative fixes the structure of effects in advance; mapping and wrapping need only Functor and Applicative.
Which statement about the IO monad is true?
Calling an IO function makes the program impure
An IO action is a box you can open to get its value
do-notation in IO turns Haskell into an imperative language
An IO action is a value describing effects, run by main
Correct! IO actions are first-class values; evaluating them is pure, and the runtime performs them only when main runs them.
An IO action is a pure value describing effects, carried out only when main runs it. There's no safe way to take the value out, and do-notation is just sugar for >>=.
How did you like the quiz?
Perfect! Every answer right!
10 of 10 right
100%
Challenge your fellow functional programmers!
Without one, it says “A friend”.
Please choose another nickname.
Question 1: In Haskell, what is the type of the bind operator (>>=)?
Question 2: What does `return 5` do inside a Haskell do-block?
Question 3: What is `Nothing >>= f` in the Maybe monad?
Question 4: What does `[1,2] >>= \x -> [x, x*10]` give?
Question 5: Which law says `return a >>= f` equals `f a`?
Question 6: Does the Haskell compiler verify that your Monad instance obeys the monad laws?
Question 7: How does the State monad keep track of state without mutation?
Question 8: Why do Haskellers use monad transformers like StateT over IO?
Question 9: What does Monad allow that Applicative alone does not?
Question 10: Which statement about the IO monad is true?
Made with AI. This quiz was made with AI, which can make mistakes.