Quiz Stuff

Quiz 4, Feburary 5, 2018
4 minutes; ½ + 1 + ½ + ½ + ½ + 1 points; 4 points total

  1. Name a Haskell type and individually write out all values of that type.
  2. Write a function f that takes a three-element list and returns a 2-tuple with the last then first element of the list. Restriction: Your solution may not use any functions! Example:
    > f [10,20,30]
    (30,10)
    
  3. What is the type of the function f from the previous question?
  4. What is the head and the tail of the list [3,4,5]?
  5. What is a very fast operation with a "cons" list? What's a slow one?
  6. What's the basic idea of patterns in Haskell?
EC ½ point: What's a case where we must use a guard instead of a pattern?

Answers

  1. Name a Haskell type and individually write out all values of that type.
    Two short answers: (1) (): (), (2) Bool: True, False
  2. Write a function that takes a three-element list and returns a 2-tuple with the last then first element of the list.
    f [x,_,z] = (z,x)
  3. What is the type of the function f from the previous question?
    f :: [b] -> (b, b)
  4. What is the head and the tail of the list [3,4,5]?
    3 and [4,5]
  5. What is a very fast operation with a "cons" list? What's a slow one?
    Fast: "Add" an element at its beginning. Slow: Get its length.
  6. What's the basic idea of patterns in Haskell?
    Bind names to elements of a data structure.
EC ½ point: What's a case where we must use a guard instead of a pattern?
A pattern can't match a range of values.