Enhance your understanding of Haskell’s core data structures with this quiz on lists, tuples, and their manipulations. Explore fundamental concepts and key operations to solidify your grasp on functional programming in Haskell.
Which of the following Haskell expressions creates a list containing the elements 1, 2, and 3?
Explanation: The correct syntax for creating a list in Haskell is to use square brackets with comma-separated elements, such as [1, 2, 3]. The option (1; 2; 3) is incorrect because semicolons and parentheses are not used to define lists. The curly braces {1, 2, 3} do not denote lists in Haskell. Finally, (1, 2, 3) is a tuple containing three elements, not a list.
What is the result type of the expression (True, 42) in Haskell?
Explanation: In Haskell, (True, 42) is a tuple of two elements: a Boolean and an integer, so its type is (Bool, Int). [Bool, Int] is syntactically incorrect and would refer to a list of mixed types, which is not allowed in Haskell. (Bool; Int) is not a valid type syntax. 'Bool, Int' is just a list of types, not a tuple type.
Given the list xs = [5, 7, 9], what does the expression head xs return?
Explanation: The head function in Haskell returns the first element of the list, so head xs will give 5. 9 is the last element, not the first. [5, 7] and [7, 9] are sublists and not single elements, and are not returned by the head function.
How can you retrieve the second element from a pair (x, y) in Haskell?
Explanation: The snd function takes a pair and returns its second element. tail is used with lists, not tuples, and would cause a type error here. head is also for lists. 'second' is not a standard Haskell function for tuples.
Which operator would you use to concatenate two lists xs and ys in Haskell?
Explanation: The ++ operator is used for concatenating lists in Haskell, so xs ++ ys joins both lists. xs + ys would result in an error as + is used for numbers. xs : ys is the cons operator and does not concatenate two lists but adds an element to the front. xs u0026 ys is not a valid list operation.
Which of the following best distinguishes a tuple from a list in Haskell?
Explanation: Tuples are heterogeneous, so their elements can be of different types; lists must be homogeneous with elements of the same type. Lists are not limited to two elements, contrary to one distractor. Lists and tuples can both store any type, not just numbers. Tuples are not restricted to length three.
How do you represent an empty list in Haskell?
Explanation: The empty list is represented by two square brackets with nothing between them, []. {} is used for other structures, not lists. 'empty' and 'null' are not valid literals; null is a function that checks for list emptiness, not an empty list.
What does the cons operator (:) do when used as 4 : [5, 6] in Haskell?
Explanation: The cons operator (:) adds its left argument to the front of the list, so 4 : [5, 6] results in [4, 5, 6]. It does not append, as that would require ++. It cannot remove elements from the list. Combining into a tuple would require different syntax.
If ys = [10, 20, 30], what is the result of length ys?
Explanation: The length function returns the number of elements in the list, so length ys gives 3. 30 is just the last element of the list, not its length. [10, 20, 30] is the list itself, not its length. 2 would be incorrect as there are three elements.
Which expression keeps only the even numbers from the list [1,2,3,4] in Haskell?
Explanation: The filter function applies a predicate and returns only elements for which it returns True, so filter even [1,2,3,4] gives [2,4]. map even [1,2,3,4] returns a list of Booleans. 'select' and 'find' are not standard Haskell list filtering functions.