CS 3040, Exercise 3: Haskell Functions

Write functions as described below. Note you will want to name the file as "Ex3.hs". The code will not be graded for style issues. Use if or pattern matching to write the functions, but do not use higher order functions like map and filter and do not use list comprehensions. You may work with others on this exercise, but each must submit their own work.

  1. Implement the (two-argument) Ackermann Function as ack. Note you probably won't want to test it on examples like ack 4 2. If you've already started it, Control-C will quit :-)!
  2. Write a function first_two which takes a list of items and returns a pair containing the first and second. You must either use patterns or head and tail in your answer; do not use !! or take. You can assume the list has at least two elements.
  3. Write a function first_even which takes a list of numbers and returns the first even number. You can use even in your solution. Your solution must be recursive; that is, do not use operations like filter or find. Assume the list has at least one even number. Note that your returned value must be a single number, not a list containing a number.
  4. Write a function swapPairs which takes a list of items and swaps every other pair. If the list has an odd length, leave the last item alone. For example, swapPairs [1, 3, 5, 8, 13] would return [3, 1, 8, 5, 13]. Again, you must implement your own recursive solution; do not use list library operations and do not use list comprehensions. That is, the only operations you can use on lists are ++, :, head, and tail.
Submit your solution to esubmit as ex3haskell. See below for details.

Hints

Submitting

Place your code in the Ex3.hs and put at the top (before any functions you define) the line

        module Ex3 where
Esubmit already has the file with a main in it, and this line allows the main file to "include" your file. The magic line says you're defining a module named "Ex3" (the capitalization is important in both the filename and the module declaration!) and that you are exporting all definitions.