Tjaz

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 (>>=)?

or press Enter ⏎
That's right!

Right! Bind takes a wrapped value and a function returning a new wrapped value, and chains them into m b.

Not quite!

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.

To get feedback you first need to answer the question above.
or press Enter ⏎

What does `return 5` do inside a Haskell do-block?

or press Enter ⏎
That's right!

Exactly! return is just pure: it lifts a value into the monad, and the lines after it still run.

Not quite!

return only wraps 5 in the monad; it doesn't exit anything. Lines after it still run, unlike return in C or Python.

To get feedback you first need to answer the question above.
or press Enter ⏎

What is `Nothing >>= f` in the Maybe monad?

or press Enter ⏎
That's right!

Correct! Maybe short-circuits: once a step gives Nothing, the rest of the chain is skipped.

Not quite!

The result is Nothing, and f is never called. Maybe models failure as a plain value, with no exceptions.

To get feedback you first need to answer the question above.
or press Enter ⏎

Scratch the card to reveal a fact about monads

or press Enter ⏎

What does `[1,2] >>= \x -> [x, x*10]` give?

or press Enter ⏎
That's right!

Yes! The List monad applies the function to every element and flattens the results.

Not quite!

The answer is [1,10,2,20]. List bind maps over every element and concatenates; [[1,10],[2,20]] would be fmap without flattening.

To get feedback you first need to answer the question above.
or press Enter ⏎

Which law says `return a >>= f` equals `f a`?

or press Enter ⏎
That's right!

Right! Left identity: wrapping a value and binding it into f is the same as calling f directly.

Not quite!

It's left identity. Right identity is m >>= return = m, and associativity concerns how nested binds group.

To get feedback you first need to answer the question above.
or press Enter ⏎

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?

or press Enter ⏎

Does the Haskell compiler verify that your Monad instance obeys the monad laws?

or press Enter ⏎
That's right!

Correct! The laws are a contract the compiler can't check; programmers prove or test them, e.g. with QuickCheck.

Not quite!

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.

To get feedback you first need to answer the question above.
or press Enter ⏎

How does the State monad keep track of state without mutation?

or press Enter ⏎
That's right!

Exactly! State s a wraps a function s -> (a, s), and bind passes each new state on to the next step.

Not quite!

State is a function s -> (a, s), and bind threads the state from step to step. Nothing is mutated; it is pure function plumbing.

To get feedback you first need to answer the question above.
or press Enter ⏎

Why do Haskellers use monad transformers like StateT over IO?

or press Enter ⏎
That's right!

Right! Functors and applicatives compose freely, but monads don't, so transformers layer one effect on another.

Not quite!

Monads don't compose in general, so transformers like StateT s IO build a stack that combines state with I/O.

To get feedback you first need to answer the question above.
or press Enter ⏎

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.

or press Enter ⏎

What does Monad allow that Applicative alone does not?

or press Enter ⏎
That's right!

Yes! With bind, the next computation can depend on the value produced before, which Applicative cannot express.

Not quite!

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.

To get feedback you first need to answer the question above.
or press Enter ⏎

Which statement about the IO monad is true?

or press Enter ⏎
That's right!

Correct! IO actions are first-class values; evaluating them is pure, and the runtime performs them only when main runs them.

Not quite!

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 >>=.

To get feedback you first need to answer the question above.
or press Enter ⏎

How did you like the quiz?

 
or press Enter ⏎

Perfect! Every answer right!

10 of 10 right

Challenge your fellow functional programmers!

Without one, it says “A friend”.

See your answers · all right
  1. Question 1:

    In Haskell, what is the type of the bind operator (>>=)?

  2. Question 2:

    What does `return 5` do inside a Haskell do-block?

  3. Question 3:

    What is `Nothing >>= f` in the Maybe monad?

  4. Question 4:

    What does `[1,2] >>= \x -> [x, x*10]` give?

  5. Question 5:

    Which law says `return a >>= f` equals `f a`?

  6. Question 6:

    Does the Haskell compiler verify that your Monad instance obeys the monad laws?

  7. Question 7:

    How does the State monad keep track of state without mutation?

  8. Question 8:

    Why do Haskellers use monad transformers like StateT over IO?

  9. Question 9:

    What does Monad allow that Applicative alone does not?

  10. Question 10:

    Which statement about the IO monad is true?

Play in 3D

Made with AI. This quiz was made with AI, which can make mistakes.

Play in 3D
Please turn on JavaScript