Haskell is a functional programming language that relies heavily on functions for computation. To excel in Haskell, understanding how to define functions, use function application and currying, compose functions, and work with higher-order functions like map, filter, and fold is crucial. These concepts power Haskell’s expressive and efficient style.
30 Multiple-Choice Questions on Functions in Haskell
Defining Functions
What is the syntax to define a function in Haskell?
A) functionName args = expression
B) def functionName(args):
C) function functionName(args)
D) let functionName = args => expression
Which keyword is used to define a local function within another function in Haskell?
A) let
B) where
C) do
D) in
How can you define a function with multiple cases in Haskell?
A) Using the if-else construct only
B) By writing multiple patterns with |
C) Using case or pattern matching
D) Using a switch statement
What happens if a function in Haskell does not cover all possible input patterns?
A) The function compiles but produces runtime errors for unhandled patterns.
B) The compiler fills the missing patterns automatically.
C) It will generate warnings but continue execution.
D) It will refuse to compile.
How do you declare a type signature for a function in Haskell?
A) functionName :: Type
B) def functionName : Type
C) let functionName = Type
D) type functionName = Type
Function Application and Currying
In Haskell, how is a function applied to its arguments?
A) By enclosing arguments in parentheses
B) By using whitespace between the function name and arguments
C) By using commas to separate arguments
D) By using a : separator
What is currying in Haskell?
A) A process of combining multiple functions into one
B) Transforming a function into a sequence of functions, each taking one argument
C) Making functions operate only on lists
D) Declaring multiple type signatures for a single function
What is the result of partially applying a function in Haskell?
A) A new function with fewer arguments
B) An error, since all arguments must be provided
C) The function is discarded
D) The partially applied function is automatically executed
What is the difference between f x y and f (x y) in Haskell?
A) f x y applies f to x and then to y, while f (x y) applies x to y first.
B) There is no difference; both are valid and equivalent.
C) f (x y) is invalid syntax.
D) f (x y) indicates that f is partially applied.
Which of the following is true about function application in Haskell?
A) It is right-associative.
B) It requires explicit parentheses for all arguments.
C) It is left-associative.
D) It requires type annotations every time.
Function Composition
What does the (.) operator do in Haskell?
A) Multiplies two numbers
B) Composes two functions such that (f . g) x = f (g x)
C) Creates a tuple of two functions
D) Concatenates two functions
How can you write the composition of f and g applied to x?
A) f g x
B) f (g x)
C) (f x) . (g x)
D) (f g) x
What is the primary benefit of using function composition in Haskell?
A) It makes the code faster.
B) It reduces the need for recursion.
C) It creates cleaner and more readable code by avoiding intermediate variables.
D) It allows functions to operate on lists directly.
How does function composition work with higher-order functions?
A) Only one higher-order function can be composed at a time.
B) Higher-order functions cannot be composed.
C) Higher-order functions can be composed just like any other functions.
D) Composition must involve intermediate variables for higher-order functions.
What is the type of the (.) operator in Haskell?
A) (a -> b) -> (b -> c) -> (a -> c)
B) (b -> a) -> (a -> c) -> (c -> b)
C) (a -> b) -> (c -> a) -> (c -> b)
D) (a -> b) -> (b -> a) -> (a -> b)
Higher-Order Functions
What is a higher-order function in Haskell?
A) A function that only operates on numbers
B) A function that takes another function as input or returns a function as output
C) A function that cannot be partially applied
D) A function that operates only on lists
Which of the following is an example of a higher-order function?
A) map
B) if-else
C) let
D) case
What does the map function do in Haskell?
A) Transforms each element of a list using a given function
B) Filters elements from a list based on a condition
C) Reduces a list to a single value
D) Creates a new list from the intersection of two lists
How does filter function work in Haskell?
A) It applies a function to all elements of a list.
B) It returns all elements of a list that satisfy a given predicate.
C) It concatenates two lists.
D) It creates a new list with unique elements only.
What is the purpose of foldl in Haskell?
A) To transform a list into a single value by iterating from left to right
B) To transform a list into a single value by iterating from right to left
C) To concatenate two lists
D) To map a function over a list
What is the key difference between foldl and foldr?
A) foldl starts from the rightmost element, while foldr starts from the leftmost element.
B) foldl processes the list left to right, while foldr processes it right to left.
C) foldl works only on numbers, while foldr works on strings.
D) There is no difference between the two.
How is map different from foldl?
A) map creates a new list, while foldl reduces the list to a single value.
B) map requires a predicate, while foldl does not.
C) foldl works only with numbers, while map works with any type.
D) foldl operates only on the last element of the list.
What is the type of the map function in Haskell?
A) (a -> b) -> [a] -> b
B) (a -> b) -> [a] -> [b]
C) (a -> b) -> a -> [b]
D) (a -> b) -> b -> [a]
How does the filter function handle an empty list?
A) It throws an error.
B) It returns Nothing.
C) It returns an empty list.
D) It concatenates two empty lists.
What does foldr (+) 0 [1, 2, 3] return in Haskell?
A) 6
B) 5
C) 3
D) 1
Which function can be used to check if all elements of a list satisfy a condition?
A) map
B) foldr
C) all
D) filter
Which higher-order function applies a function to elements of two lists simultaneously?
A) map
B) zipWith
C) filter
D) foldl
What does foldl (*) 1 [1, 2, 3] return?
A) 6
B) 5
C) 1
D) 0
How does foldl differ from scanl?
A) foldl produces intermediate results, while scanl does not.
B) scanl
4o
ChatGPT said:
ChatGPT
produces a list of accumulated results, while foldl only returns the final result.
C) There is no difference; they are equivalent.
D) scanl is used for numerical operations only.
What is the result of applying map (+1) to the list [1,2,3] in Haskell?
A) [1, 2, 3]
B) [2, 3, 4]
C) [1, 3, 6]
D) [3, 2, 1]
Answers:
Qno
Answer (Option with the text)
1
A) functionName args = expression
2
A) let
3
B) By writing multiple patterns with `
4
A) The function compiles but produces runtime errors for unhandled patterns
5
A) functionName :: Type
6
B) By using whitespace between the function name and arguments
7
B) Transforming a function into a sequence of functions, each taking one argument
8
A) A new function with fewer arguments
9
A) f x y applies f to x and then to y, while f (x y) applies x to y first
10
A) It is right-associative.
11
B) Composes two functions such that (f . g) x = f (g x)
12
B) f (g x)
13
C) It creates cleaner and more readable code by avoiding intermediate variables
14
C) Higher-order functions can be composed just like any other functions
15
A) (a -> b) -> (b -> c) -> (a -> c)
16
B) A function that takes another function as input or returns a function as output
17
A) map
18
A) Transforms each element of a list using a given function
19
B) It returns all elements of a list that satisfy a given predicate
20
A) To transform a list into a single value by iterating from left to right
21
B) foldl processes the list left to right, while foldr processes it right to left
22
A) map creates a new list, while foldl reduces the list to a single value
23
B) (a -> b) -> [a] -> [b]
24
C) It returns an empty list
25
A) 6
26
C) all
27
B) zipWith
28
A) 6
29
B) scanl produces a list of accumulated results, while foldl only returns the final result