Due Wed. 4 February 1998
(which effectively means Thursday midnight with your automatic grace)
This assignment is 40 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, but not guaranteed, that the following set of functions, along with ones you compose, is adequate to do all of the oproblems.
first second third keep drop map mappend range rest some sort == < remove_duplicates length float reduce pow
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 constructed 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. When the problem states that a function's argument has a given form, you may assume that the test cases will not try any other forms; that is, you do not need to put in error-checking for erroneous argument types.
0. Construct a rex function sum having a list of numbers as an argument which returns the sum of the list of numbers.
test(sum(range(1, 10)), 55);
1. Construct a rex function average having a list of numbers as an argument and which returns the numeric average of the list of numbers. You may wish to apply the function float to the length of the list before dividing by it, so that the result of division does not truncate in the case that the list consists only of integers.
test(average([1, 2, 3, 4, 6]), 3.2);
2. Construct a rex predicate equalAsSets having two arguments which tells whether two list arguments have the same elements, when ordering and replication are ignored.
test(equalAsSets([1, 3, 5, 1], [5, 1, 5, 3, 3]), 1);
test(equalAsSets([3, 5, 1], [5, 3, 4]), 0);
3. Construct a rex function powers which creates a list of the first through tenth powers of its argument.
test(powers(2), [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]);
4. Construct a rex function powRange which creates a list of the first through tenth powers of the range of numbers from the first argument to the second.
test(powRange(1, 5), [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024], [3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049], [4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144, 1048576], [5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625] ]);
5. Construct a rex function sumBoth which produces, as a list of two elements, the sum of the positive numbers and the sum of the negative numbers in a non-empty list.
test(sumBoth(range(-10, 5)), [15, -55]);
The following questions concern the flights database discussed in class. The full database will be available on turing in /cs/cs60/a/flights.rex. The test cases will conform to the contents of that database, and will not necessarily be the same as shown below.
6. Construct a rex function nextDay which determines, for a list of flights between two cities, whether any flight has an arrival time earlier than its departure time (which presumably means that it arrives the next day).
test(nextDay([[472, [20, 30], [21, 45]], [1201, [16, 25], [00, 05]]]), 1); test(nextDay([[472, [20, 30], [21, 45]]]), 0);
7. Construct a rex function overnight which applies to one argument, the list of all flights in the database. It returns the list of pairs of cities for which there is a flight satisfying predicate nextDay in the previous problem.
test(overnight(flights), ["ONT", "STL"]);