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. Vector Sum

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

3. Dot Product

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

4. Matrix Addition

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

5. All Pairs

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

6. Mergesort

Write a function that sorts a list using mergesort.

7. Linear Congruential Generator

A linear congruential generator is a formula for generating a series of pseudorandom numbers. It has the form

Xn+1 = (a Xn + c) mod m

where X is the series of pseudorandom values, and a, c, and m are constants.

Use a linear congruential generator to construct an infinite list of values of type Double in the range from 0.0 to 1.0. Use the constants