Haskell Monads and Functors Essentials Quiz Quiz

Explore key concepts related to Monads and Functors in Haskell with beginner-friendly questions covering type classes, syntax, and practical examples. Strengthen your foundational understanding of how Monads and Functors work, their laws, and their applications in functional programming.

  1. Functor Type Class

    Which Haskell type class provides the fmap function to apply a function over values wrapped in a context, such as Just 3?

    1. Foldable
    2. Applicative
    3. Monad
    4. Functor

    Explanation: The Functor type class provides the fmap function, allowing you to apply a function to values inside a structure like Maybe or List. Monad and Applicative offer different functionalities such as binding and sequencing, but not fmap directly. Foldable deals with reducing data structures to a single value, making Functor the only correct choice for fmap.

  2. Functor fmap Example

    What is the result of fmap (+1) (Just 4) in Haskell?

    1. Just 4
    2. Nothing
    3. Just 5
    4. 5

    Explanation: The expression fmap (+1) (Just 4) applies the function (+1) to the value inside Just, resulting in Just 5. Selecting 5 ignores the context, and Nothing is incorrect as the value is present. Just 4 would be the result if the function had no effect, but here it is clearly incremented.

  3. Functor Law: Identity

    According to the Functor laws, what should fmap id x always return for any functor x?

    1. Nothing
    2. id x
    3. Just x
    4. x

    Explanation: The Functor identity law states that fmap id over any functor should yield the original functor. Choosing id x confuses the function with its application. Nothing and Just x only make sense with Maybe, and are not general enough, making x the only acceptable answer.

  4. Monad's Fundamental Operation

    Which operation forms the core of the Monad type class, sequencing computations that produce wrapped values?

    1. foldr
    2. (u003Eu003E=)
    3. fmap
    4. pure

    Explanation: The bind operator (u003Eu003E=) is fundamental to Monads, enabling chaining of operations on wrapped values. fmap belongs primarily to Functor, pure is an Applicative function, and foldr is used for folding structures, not sequencing monadic actions.

  5. Return in Monads

    In Haskell, what does the function return do in the context of Monads?

    1. Returns the last value in a list
    2. Wraps a value into a monadic context
    3. Exits a function early
    4. Unwraps a value from a monad

    Explanation: The return function places a regular value into a minimal monad, like turning 7 into Just 7 for Maybe. It does not exit functions early or return list elements, nor does it unwrap values. Unwrapping is handled by different functions depending on the monad.

  6. do Notation

    What is the purpose of do notation in Haskell when working with Monads?

    1. To write sequences of monadic actions in a clearer, imperative style
    2. To import modules
    3. To declare type signatures
    4. To define new type classes

    Explanation: do notation improves readability when chaining monadic operations, mimicking imperative programming. It is not related to type class definitions, importing modules, or declaring type signatures, all of which are achieved with distinct syntactical constructs.

  7. Applicative vs Functor

    Which feature does the Applicative type class add that Functor does not provide?

    1. Sequencing monadic computations
    2. Applying wrapped functions to wrapped values
    3. Mapping a function over a list
    4. Folding a data structure

    Explanation: Applicative allows you to apply functions within a context to values within a context, extending beyond Functor’s abilities. Mapping over a list is available through Functor, folding is for Foldable, and sequencing is characteristic of Monad, not Applicative.

  8. Monad Law: Left Identity

    Which equation illustrates the left identity law for Monads in Haskell?

    1. return a u003Eu003E= f == f a
    2. fmap id == id
    3. m u003Eu003E= return == m
    4. pure f u003C*u003E x == fmap f x

    Explanation: The left identity law says that using return followed by bind to a function should be the same as applying the function directly. m u003Eu003E= return == m is the right identity, fmap id == id is a Functor law, and pure f u003C*u003E x == fmap f x is for Applicatives.

  9. Maybe as a Monad

    Why is the Maybe type a commonly used Monad in Haskell?

    1. It represents computations that can fail with Nothing or produce a value with Just
    2. It creates lists of results
    3. It is the default type for all functions
    4. It can only hold numbers

    Explanation: Maybe models computations that might fail, using Nothing for failure and Just for success, making it a practical Monad. It is not limited to numbers, does not generate lists, and is not a universal default type. These other options do not describe the core utility of Maybe.

  10. Functor Mapping on Lists

    What is the output of fmap (*2) [1,2,3] in Haskell?

    1. [2,3,4]
    2. [1,4,9]
    3. [2,4,6]
    4. [1,2,3]

    Explanation: fmap (*2) doubles each element in the list, resulting in [2,4,6]. Choosing [1,2,3] ignores the transformation, [2,3,4] and [1,4,9] would be produced with other functions, not the doubling operation in the question.