Essentials of Lists, Tuples, and Data Structures in Haskell Quiz

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.

  1. Identifying a List Creation

    Which of the following Haskell expressions creates a list containing the elements 1, 2, and 3?

    1. [1, 2, 3]
    2. (1; 2; 3)
    3. (1, 2, 3)
    4. {1, 2, 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.

  2. Basic Tuple Syntax

    What is the result type of the expression (True, 42) in Haskell?

    1. (Bool; Int)
    2. (Bool, Int)
    3. Bool, Int
    4. [Bool, Int]

    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.

  3. Accessing List Elements

    Given the list xs = [5, 7, 9], what does the expression head xs return?

    1. 5
    2. [7, 9]
    3. [5, 7]
    4. 9

    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.

  4. Tuple Indexing

    How can you retrieve the second element from a pair (x, y) in Haskell?

    1. head (x, y)
    2. snd (x, y)
    3. tail (x, y)
    4. second (x, y)

    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.

  5. Combining Lists

    Which operator would you use to concatenate two lists xs and ys in Haskell?

    1. xs : ys
    2. xs ++ ys
    3. xs + ys
    4. xs u0026 ys

    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.

  6. Difference Between Lists and Tuples

    Which of the following best distinguishes a tuple from a list in Haskell?

    1. A tuple can hold elements of different types, but a list must contain elements of the same type.
    2. Tuples must always be of length three, lists can be any length.
    3. Lists can only store numbers, tuples can store any type.
    4. A list always has exactly two elements, while a tuple can have any number.

    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.

  7. Empty List Identification

    How do you represent an empty list in Haskell?

    1. empty
    2. null
    3. []
    4. {}

    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.

  8. Cons Operator Purpose

    What does the cons operator (:) do when used as 4 : [5, 6] in Haskell?

    1. Appends 4 at the end of the list [5, 6]
    2. Combines 4 and [5, 6] into a tuple
    3. Removes 4 from the list if present
    4. Adds 4 to the front of the list [5, 6], making [4, 5, 6]

    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.

  9. Checking List Length

    If ys = [10, 20, 30], what is the result of length ys?

    1. 2
    2. 3
    3. 30
    4. [10, 20, 30]

    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.

  10. Filtering Lists

    Which expression keeps only the even numbers from the list [1,2,3,4] in Haskell?

    1. select even [1,2,3,4]
    2. filter even [1,2,3,4]
    3. map even [1,2,3,4]
    4. find even [1,2,3,4]

    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.