Functional programming in Haskell is centered around using design patterns like Functors, Applicatives, Monads, lenses, and traversals to write clean, modular code. By mastering these concepts, along with fold/unfold operations and algebraic data types (ADTs), developers can create highly reusable and maintainable code. This set of 30 multiple-choice questions will help you test your understanding of these core functional programming patterns and how they can be leveraged for designing efficient, clean Haskell programs.
Functional Programming Design Patterns in Haskell – MCQs
Part 1: Functors, Applicatives, and Monads Revisited
What is the key property of a Functor in Haskell?
A) It applies a function to the value inside a container.
B) It modifies the structure of the data inside a container.
C) It allows the chaining of computations.
D) It creates a new container from an existing one.
Which of the following is a method defined for Functors?
A) map
B) fmap
C) apply
D) join
What is the main difference between a Functor and an Applicative in Haskell?
A) Functors only allow applying functions to values, while Applicatives allow applying functions wrapped in a context.
B) Functors allow chaining computations, while Applicatives do not.
C) Applicatives are less flexible than Functors.
D) Functors work only with lists, while Applicatives work with any data type.
Which function is used to apply a wrapped function to a wrapped value in the context of Applicatives in Haskell?
A) map
B) fmap
C) apply
D) (<*>)
What is the purpose of a Monad in Haskell?
A) To handle side effects in a pure functional way.
B) To store multiple values of different types.
C) To structure data as a sequence of computations.
D) To define immutable data types.
Which method is commonly used for chaining operations in Monads?
A) map
B) flatMap
C) bind (>>=)
D) fold
What is the result of applying the >>= operator in Haskell’s Monad instance?
A) It allows the result of a computation to affect the subsequent computation.
B) It maps a function over a value.
C) It filters out invalid results.
D) It combines two monads into a new one.
Which of the following is NOT an instance of a Monad in Haskell?
A) Maybe
B) List
C) IO
D) Int
What is the return function in the context of Monads?
A) It wraps a value into a Monad.
B) It returns the value inside a Monad.
C) It produces a monadic value from two values.
D) It terminates a computation.
In which situation would you use the Just constructor with the Maybe Monad in Haskell?
A) When there is a computation that may return a result.
B) When you need to chain computations without failure.
C) When a function always returns a result.
D) When you want to handle multiple results.
Part 2: Designing with Lenses and Traversals
What is the purpose of a Lens in Haskell?
A) To modify immutable data structures in a functional way.
B) To create mutable references to data.
C) To traverse and manipulate elements in a list.
D) To define higher-order functions for recursion.
Which Haskell package provides Lens functionality?
A) lens
B) control
C) containers
D) monad
How do lenses help in functional programming design in Haskell?
A) By providing an easy way to focus on parts of immutable data structures.
B) By allowing stateful computation in a pure way.
C) By making all data structures mutable.
D) By optimizing memory usage in large datasets.
What does the set function do in the context of lenses?
A) It modifies the value inside the lens.
B) It retrieves the value inside the lens.
C) It transforms the value inside the lens.
D) It compares the value inside the lens.
What is a traversal in Haskell used for?
A) To iterate over all elements in a data structure.
B) To find an element in a list.
C) To fold a data structure into a single value.
D) To manipulate all the values in a structure in parallel.
Which of the following best describes the role of lenses and traversals in Haskell?
A) They allow for focusing on specific parts of a complex data structure.
B) They implement algorithms for sorting data.
C) They define types for monads and applicatives.
D) They enable mutable references in an immutable setting.
Which of the following can lenses be composed with in Haskell?
A) Functors
B) Monads
C) Applicatives
D) All of the above
What type of value does the traverse function return?
A) A transformed list
B) A monadic result
C) A new immutable data structure
D) A foldable structure
What is the result of applying a traversal on a structure?
A) A new structure with modified elements
B) A sequence of side effects
C) A single value representing all the elements
D) A mutable version of the structure
What is the key benefit of using lenses and traversals over traditional getter and setter methods in Haskell?
A) Lenses and traversals allow for modular and reusable code without breaking immutability.
B) They provide mutable references to parts of data structures.
C) They simplify the recursive definition of data types.
D) They allow for type safety in mutable environments.
Part 3: Working with Fold and Unfold
What is the general purpose of a fold function in Haskell?
A) To accumulate a value from a data structure
B) To convert a value into a monadic result
C) To apply a function to every element in a structure
D) To traverse a data structure in reverse
Which of the following is an example of a fold function in Haskell?
A) foldl
B) traverse
C) mapM
D) pure
What is the difference between foldr and foldl in Haskell?
A) foldr folds from the right, while foldl folds from the left.
B) foldr is faster than foldl.
C) foldl is used with monads, and foldr is not.
D) There is no difference between foldr and foldl.
What is the role of unfold in Haskell?
A) It generates a data structure from a single value.
B) It reverses a data structure.
C) It reduces a data structure to a single value.
D) It maps a function over a list of values.
Which of the following is the type signature of a foldr function in Haskell?
A) foldr :: (a -> b -> b) -> b -> [a] -> b
B) foldr :: (a -> b -> a) -> [a] -> b
C) foldr :: (a -> a -> b) -> [a] -> a
D) foldr :: (b -> a -> b) -> a -> [a] -> a
What is an example of a situation where unfold is useful in Haskell?
A) When you need to generate an infinite list based on a function.
B) When you want to fold a list into a single value.
C) When you need to perform side effects in a pure way.
D) When you want to define a data structure recursively.
Which of the following is true about the unfoldr function in Haskell?
A) It constructs a list by applying a function that generates values and stops when the function produces Nothing.
B) It constructs a tree structure from a list of values.
C) It accumulates values in a list based on a recursive function.
D) It performs a left fold on a list of values.
What is the result of applying foldr (+) 0 [1,2,3] in Haskell?
A) 6
B) 0
C) 3
D) 9
In Haskell, which function would you use to unfold a list from a seed value?
A) unfoldr
B) foldl
C) map
D) zipWith
What is the key benefit of using fold and unfold in functional programming design?
A) They provide a declarative way to define data transformations.
B) They mutate data in a controlled way.
C) They allow for mutable state handling in a functional environment.
D) They simplify the use of side effects.
Answer Key
Qno
Answer
1
A) It applies a function to the value inside a container.
2
B) fmap
3
A) Functors only allow applying functions to values, while Applicatives allow applying functions wrapped in a context.
4
D) (<*>)
5
C) To structure data as a sequence of computations.
6
C) bind (>>=)
7
A) It allows the result of a computation to affect the subsequent computation.
8
D) Int
9
A) It wraps a value into a Monad.
10
A) When there is a computation that may return a result.
11
A) To modify immutable data structures in a functional way.
12
A) lens
13
A) By providing an easy way to focus on parts of immutable data structures.
14
A) It modifies the value inside the lens.
15
A) To iterate over all elements in a data structure.
16
A) They allow for focusing on specific parts of a complex data structure.
17
D) All of the above
18
B) A monadic result
19
A) A new structure with modified elements
20
A) Lenses and traversals allow for modular and reusable code without breaking immutability.
21
A) To accumulate a value from a data structure
22
A) foldl
23
A) foldr folds from the right, while foldl folds from the left.
24
A) It generates a data structure from a single value.
25
A) foldr :: (a -> b -> b) -> b -> [a] -> b
26
A) When you need to generate an infinite list based on a function.
27
A) It constructs a list by applying a function that generates values and stops when the function produces Nothing.
28
A) 6
29
A) unfoldr
30
A) They provide a declarative way to define data transformations.