Harvey Mudd College

Computer Science 60

Fall 1997

Assignment 1

Due Friday 19 September 1997

Exploring the use of Functions

This assignment is 30 points of your overall grade. Each problem is 5 points.

You are asked to construct several function definitions in rex and test them. You should submit your functions in a single file clearly commented. You don't have to write a book on each one, but at least describe your approach.

You will want to have read Chapter 3, as only the first problem is workable with the material in Chapter 2 alone. You may want to have tried writing some simpler functions on your own as well in preparation. It is believed that the following set of functions, along with ones you compose, is adequate to do all of the problems.

==    *    %    all    first    keep    map    range    rest    some    sort

The test predicate used below is built into rex and has the form

test(Expression, Answer);

It evaluates the Expression and tells you whether or not the result is the same as Answer (by printing "ok" or "bad"). We present the examples in this form so that you can just copy the lines out of this file. A file with more extensive tests will be found on turing in ~cs/cs60/a/a1.rex. You might want to copy this file and embed your solutions in it to save typing in the tests.

The graders will test your definitions on these and other examples.

1. Construct a rex predicate isPerm of two arguments which tells whether one argument is a permutation of the other.

test(isPerm([2, 1, 5, 4, 3, 6], [6, 2, 1, 3, 4, 5]), 1);

test(isPerm([2, 1, 5, 4, 3, 6], [6, 2, 1, 7, 4, 5]), 0);

 

2. A multiplication table (in this problem, table for short) is a matrix obtained by multiplying all combinations of the elements in two lists of numbers. Each row of the matrix is the product of all of the elements of the first list by the elements of the second list. Specify in rex a function tabulate which produces a such a table. For example:

    test(tabulate([0, 1, 2, 3, 4], [0, 1, 2, 3, 4]),

[ [0, 0, 0,  0,  0],

 [0, 1, 2,  3,  4],

 [0, 2, 4,  6,  8],

 [0, 3, 6,  9, 12],

 [0, 4, 8, 12, 16] ] );

(The formatting was done after-the-fact. Don't worry if your output is all on one line.) Hint: First figure out how to multiply all of the second list by a given number, then go from there.

3. Create a three-argument version of tabulate which takes any binary function as its first argument. (rex allows you to use the same name for different functions, as long as they have a different number of argments.) For example:

    test(tabulate(-, [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]),

    [ [0, -1, -2, -3, -4],

      [1,  0, -1, -2, -3],

      [2,  1,  0, -1, -2],

      [3,  2,  1,  0, -1],

      [4,  3,  2,  1,  0] ] );

4. Define the function modTable to produce a multiplication table for the integers modulo N. Multiplication modulo N means computing the ordinary product then taking the result % N (M %N is the remainder after dividing M by N). For example, 3*5 modulo 7 is 1, since the remainder of dividing 15 by 7 is 1. An example is:

   test(modTable(3), [[0, 0, 0], [0, 1, 2], [0, 2, 1]]);

5. As it turns out, multiplication mod N is a "field" provided that each row of its table, other than the first all-zero row, contains a 1. Construct a predicate isField such that isField(N) indicates whether multiplication mod N is a field. For example,

test(isField(3), 1); // mod 3 multiplication is a field

test(isField(4), 0); // mod 4 multiplication is not.

6. Write a rex expression which gives the integers N between 2 and 20 such that multiplication modulo N is a field. Do you notice anything interesting about those numbers? (Put your answer to this in a comment.)