Week 4: Exercises

1. Insertion Sort

Write a predicate that sorts a list of integers. Use an insertion sort.

2. Permutations

Write a predicate permutation(L, M) that is true if the list L is a permutation of M.

3. Combinations

Write a predicate combination(L, N, M) that is true if M is a combination of N elements of L, i.e. a subset of size N that has its elements in the same order as in L.

4. Merge Sort

Write a predicate that sorts a list of integers. Use a merge sort.

5. Quicksort

Write a predicate that sorts a list of integers. Use a quicksort.

6. Digits

Write a predicate digits(L, N) that converts a list of digits (e.g. [3, 4, 5]) to an integer (e.g. 345).

7. Vectors

We can represent a vector as a list of floating-point numbers. Write predicates that can calculate the following:

  1. the sum of two vectors

  2. multiplying a scalar by a vector

  3. the dot product of two vectors

  4. the angle between two vectors

All predicates should work in any direction.

8. Matrices

We can represent a matrix as a list of lists of floating-point numbers.

Write a predicate that tests whether a matrix has dimensions N x N, for a given N. Also write predicates that can calculate the following:

  1. the zero matrix of a given size

  2. the sum of two matrices

  3. the first column of a matrix (a vector)

  4. the identity matrix of a given size

  5. the transpose of a matrix

  6. the product of two matrices

All predicates should work in any direction.