Week 7: Exercises

1. Function Types

What are the types of these functions?

  1. triple x = 3 * x

  2. even x = (x `mod` 2 == 0)

  3. palindrome xs = (reverse xs == xs)

  4. f x = f x

2. Mergesort

Write a function that sorts a list using mergesort.

3. Quicksort

Write a function that sorts a list using quicksort.

4. Vector Sum

Write a function add_vec that adds two vectors represented as lists. Give your function the most general possible type.

5. Dot Product

Write a function dot that computes the dot product of two vectors represented as lists.

6. Matrix Addition

Write a function add that adds two matrices represented as lists of lists.

7. All Pairs

Construct an infinite list allPairs that contains all pairs of positive integers. Every pair must appear exactly once in the list.

8. Floor

The RealFrac type class includes a function floor that rounds a value down to the nearest integer:

floor :: (RealFrac a, Integral b) => a -> b

Assume that floor is not predefined, and write it yourself.