attern matching and guards are powerful features in Haskell that allow for concise and expressive code. Pattern matching is often used with data types like lists, tuples, and custom types to simplify condition checking. Guards and where clauses help in defining more readable conditions for function definitions, while recursive pattern matching allows for cleaner and more efficient recursion. “As patterns” are used for enhancing readability when dealing with complex structures. These topics are fundamental for mastering Haskell’s functional programming style.
MCQs on Pattern Matching and Guards in Haskell Programming
Pattern Matching on Data Types
What is the output of the following Haskell function for the input [1,2,3]?lessCopy codef (x:xs) = x f [] = 0 A) 1 B) 2 C) 3 D) 0
Which of the following is true about pattern matching on a tuple in Haskell? A) Only the first element of the tuple can be matched B) Pattern matching can be used to extract multiple elements C) Tuples are not supported in pattern matching D) Tuples can only be matched using guards
How does Haskell pattern match on an empty list []? A) It is considered a base case B) It causes a runtime error C) It matches as any other element D) It matches only when the list has a single element
In Haskell, what is the output of the following function call f (1, 2, 3)?javaCopy codef (x, y, z) = x + y + z f _ = 0 A) 0 B) 3 C) 6 D) 2
Which of the following is correct regarding pattern matching on custom data types in Haskell? A) Pattern matching can only be done on primitive types B) It cannot be used for data types with multiple constructors C) Pattern matching is a natural way to handle custom types with multiple data constructors D) Custom data types cannot be pattern matched
Guards and Where Clauses
What is the purpose of a guard in Haskell? A) To ensure that a pattern is matched B) To check conditions when defining functions C) To bind variables to specific values D) To prevent errors during pattern matching
What does the following Haskell code do?Copy codeabsValue x | x >= 0 = x | otherwise = -x A) Returns the absolute value of x B) Returns the negative of x C) Returns the square of x D) Returns x unchanged
How does the where clause help in Haskell? A) It defines default values for patterns B) It can define local variables to be used in the function C) It acts as a guard for specific conditions D) It forces the use of recursion in functions
Which of the following is an example of using a guard in Haskell? A) f x = x + 2 B) f x | x > 0 = "Positive" | otherwise = "Non-positive" C) f (x:xs) = x D) f = x
What is the result of calling f 3 in the following function?
f x | x > 0 = "Positive" | x == 0 = "Zero" | otherwise = "Negative"
A) “Positive” B) “Zero” C) “Negative” D) Error
Recursive Pattern Matching and Optimization
What is the output of the recursive function call f [1, 2, 3] where f is defined as:
f (x:xs) = x + f xs f [] = 0
A) 6 B) 3 C) 1 D) 0
What happens if you omit a recursive base case in a Haskell function? A) The function will be optimized B) It will cause an infinite recursion leading to a stack overflow C) It will always return Nothing D) The program will terminate successfully
How can recursion be optimized in Haskell for better performance? A) By avoiding pattern matching B) By using tail recursion and accumulating results C) By using where clauses in recursion D) By calling the function only once
Which of the following best describes a tail-recursive function in Haskell? A) A function where the recursive call is the first operation in the function body B) A function that never calls itself recursively C) A function that uses pattern matching exclusively D) A function that calls other functions instead of itself
What is the result of evaluating f 4 if the function f is defined recursively as follows?
f 0 = 1 f n = n * f (n-1)
A) 4 B) 24 C) 1 D) 120
Using As Patterns for More Readable Code
What does the @ symbol in Haskell represent in pattern matching? A) It is used to denote a list B) It binds a variable to the entire matched value for further use C) It defines a guard condition D) It defines a type constraint
What is the benefit of using “as patterns” in Haskell? A) It helps avoid unnecessary recursion B) It increases the readability of the code C) It restricts the function to certain patterns D) It prevents the use of tuples
In the following Haskell function, what does the @ in xs@(x:xs) do?
f xs@(x:xs) = x f [] = 0
A) It matches the value of x and xs separately B) It binds xs to the entire list for future use C) It matches only when the list has more than one element D) It checks if the list contains exactly two elements
What would be the result of calling f [1,2,3] with the following function?
f xs@(x:xs) = length xs f [] = 0
A) 2 B) 3 C) 1 D) 0
How does “as patterns” improve code readability? A) By avoiding pattern matching entirely B) By allowing you to refer to the entire matched value while also destructuring it C) By limiting the use of recursion D) By simplifying guards
Combining Pattern Matching and Guards
What will be the output of f 10 if the following function is used?
f x | x > 5 = "Greater" | x <= 5 = "Lesser"
A) “Greater” B) “Lesser” C) “Equal” D) Error
How does Haskell decide between different patterns when multiple match clauses are present? A) It uses a first-match rule B) It always chooses the most complex pattern C) It follows a depth-first matching strategy D) It chooses the pattern with the highest complexity
Which of the following is an advantage of using guards in Haskell? A) Guards allow for more concise and readable code B) Guards prevent recursion C) Guards automatically optimize pattern matching D) Guards only work with custom data types
Which of the following expressions is an example of using a “where” clause for optimization in Haskell? A) f x = g x + 1 where g x = x * 2 B) f x = g x | x > 0 = True | otherwise = False C) f x = x + 1 | x < 10 = True D) f x = x + 1
What is the result of f 5 if f is defined as follows?
f x | x < 0 = "Negative" | x == 0 = "Zero" | x > 0 = "Positive"
A) “Negative” B) “Zero” C) “Positive” D) Error
What does the following pattern match return when called with f [3, 2, 1]?
f (x:y:xs) = x + y f [] = 0
A) 6 B) 5 C) 3 D) 2
Which of the following is the most efficient way to match on a list in Haskell? A) Using recursion with a base case and pattern matching B) Using guards only C) Using “as patterns” D) Using infinite lists
How would you match on a list where you want both the first element and the rest of the list in Haskell? A) Use x:xs B) Use x|xs C) Use head xs D) Use first:rest
What does this code return for input [3, 2, 1]?
f xs@(x:xs) = length xs f [] = 0
A) 3 B) 2 C) 1 D) 0
What will the following Haskell function return for f [1,2,3]?
f (x:xs) = "First element: " ++ show x f [] = "Empty list"
A) “First element: 1” B) “First element: 2” C) “Empty list” D) Error
Answers
Qno
Answer
1
A) 1
2
B) Pattern matching can be used to extract multiple elements
3
A) It is considered a base case
4
C) 6
5
C) Pattern matching is a natural way to handle custom types with multiple data constructors
6
B) To check conditions when defining functions
7
A) Returns the absolute value of x
8
B) It can define local variables to be used in the function
9
B) `f x
10
A) “Positive”
11
A) 6
12
B) It will cause an infinite recursion leading to a stack overflow
13
B) By using tail recursion and accumulating results
14
A) A function where the recursive call is the first operation in the function body
15
B) 24
16
B) It binds a variable to the entire matched value for further use
17
B) It increases the readability of the code
18
B) It binds xs to the entire list for future use
19
A) 2
20
B) By allowing you to refer to the entire matched value while also destructuring it
21
A) “Greater”
22
A) It uses a first-match rule
23
A) Guards allow for more concise and readable code
24
A) f x = g x + 1 where g x = x * 2
25
C) “Positive”
26
B) 5
27
A) Using recursion with a base case and pattern matching