CS 222

Logo

Programming Languages

Haskell Activity 4 - Pattern Matching and Functional Loops

Open the comand prompt / terminal, navigate to your desktop, and enter ghci. Open a code editor (e.g., Atom, Visual Studio Code, Sublime Text) and create a file activity04.hs; save it to the cs222 folder on your desktop. As you write functions, test them in ghci.

It is expected that you have read Ch. 4 of your Haskell book.

  1. Copy and paste the following into your file. For each function (test1 through test6), what will happen when the function is called with different arguments? Does it succeed, and if so, what are the resulting bindings (values of the given parameters)? Test it out and write your answers (a couple sentences) in comments above the function.
    -- YOUR ANSWER:
    test1 1 = 1
    test1 x = 42
    
    -- YOUR ANSWER:
    test2 (x:y) = (x,y)
       
    -- YOUR ANSWER:
    test3 (x:y:z) = (x,y,z)
       
    -- YOUR ANSWER:
    test4 x = 4
       
    -- YOUR ANSWER:
    test5 [] = error "bad input"
    test5 [1] = error "not horrible input but also not great"
    test5 [1,x] = x
    test5 (h : t) = h
       
    -- YOUR ANSWER:
    test6 (x,y) = x
    
  2. Write a function twoHeads :: [a] -> (a,a) that uses pattern matching to produce a tuple consisting of the first two items of a list. It should produce an error if the list is empty or has only 1 element. For example, calling the function should produce the following output:
    *Main> twoHeads []
    *** Exception: Can't take two heads of an empty list
    ...
    *Main> twoHeads [1]
    *** Exception: Can't take two heads of a list of size 1
    ...
    *Main> twoHeads [1..5]
    (1,2)
    *Main> twoHeads [5..11]
    (5,6)
    
  3. There is a special electronic calculator described by Douglas Adams as one in which any number in the entire world can be calculated, as long it is less than 4. Otherwise the result is “A suffusion of yellow”. Simulate a simple version of this calculator by creating a function dAdams :: Int -> String so that it returns “one”, “two”, “three”, or “A suffusion of yellow” depending on the input integer. Do so using pattern matching and NO if statements.

  4. Write a function mergeSortedLists :: [Int] -> [Int] -> [Int] which takes two lists of integers and merges them into one sorted list consisting of all integers from both lists. Use pattern matching and recursion to process the input lists, and don’t forget to handle the case that either list is empty. For example, here is the expected output:
    *Main> mergeSortedLists [1,3,4,5,7] [2,3,6,7,8,9]
    [1,2,3,3,4,5,6,7,7,8,9]
    
  5. Use pattern matching and recursion to write a function myZip :: [a] -> [b] -> [(a,b)] which takes two lists (each of any type) and zips them together into a list of tuples, where the first of the tuple is from the first list and the second is from the second list. You can truncate the remainder of the larger list (hint: handle the case that one list is empty). For example, here is the expected output:
    *Main> myZip [1..5] ['a'..'z']
    [(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e')]
    *Main> myZip "haskell" "good"
    [('h','g'),('a','o'),('s','o'),('k','d')]
    

How to submit

Submit your file to Moodle which has all working function definitions.