EXERCISE SET #8

1. Define a Scheme procedure arity that takes any number of arguments and returns the number of arguments it is given.

2. Define a Scheme procedure display-integer-in-field that takes two arguments, an integer n and a natural number minimum-field-width, and displays n in a field of no fewer than minimum-field-width columns, padding it with spaces on the left if necessary to bring it up to the specified width. (If the numeral for the integer requires more than the specified number of columns, the procedure should display it in full anyway.)

Next, modify the procedure so that it can (optionally) take a third argument indicating the port to which the output should be written. If only two arguments are supplied, the procedure should write the output to the current output port.

3. Define a Scheme procedure vector-sum that returns the sum of the elements of a given vector of numbers.

4. Define a Scheme procedure add-vectors that takes two vectors of numbers, equal in length, as arguments and returns a vector containing their (componentwise) sum. For example, the call (add-vectors '#(3 5 7 9) '#(3 1 4 1)) should return #(6 6 11 10).

5. Define a Scheme procedure vector-map that takes as arguments a procedure proc of arity 1 and a vector vec and returns a vector, of the same length as vec, in which the elements are constructed by applying proc to the corresponding elements of vec. For example, the call (vector-map odd? '#(3 1 4 1 6)) should return #(#t #t #f #t #f).

6. Define a Scheme procedure vector-append that takes any number of arguments, each of which must be a vector, and returns a single vector containing all of the elements of all the given vectors, in effect concatenating those vectors.

7. The file /u2/stone/events/scheme-workshop/examples/gps.scm contains the source code for a sketchy version of Newell and Simon's General Problem Solver. Examine this program and describe the problem-solving strategy that the General Problem Solver uses.

8. A hungry monkey is in a room that contains a bunch of bananas, suspended from the ceiling (out of the monkey's reach) by a rope, and a chair, light enough to be moved. The bananas are in the middle of the room, while the chair is against the north wall, far enough from the middle of the room that the monkey cannot reach the bananas even by standing on the chair if it remains in its current position. Select a set of operations available to the monkey and a set of conditions that can be used to describe various states of the monkey's environment. Describe the initial state and the goal state in terms of these conditions. Submit the data to the GPS procedure and see whether it can solve the monkey's problem (its goal is not to be hungry).

9. A man wishes to transport a wolf, a goat, and a cabbage across a river. He finds a sound boat that he can row easily, but there is room in the boat for only one of the three pieces of freight (and the man). The wolf and the goat may not be left together on the same side of the river in the absence of the man, since the wolf would then eat the goat. Similarly, the goat and the cabbage may not be left together on the same side of the river, since the goat would eat the cabbage. The problem is to find a way for the man to get all three items across the river. It is acceptable for the boat to be left on the bank opposite the one it starts out on.

Can GPS solve this problem? It depends on details of the way the operations are defined. What operations would you provide in order to enable GPS to solve the problem?

10. The current version of the GPS can fail to find a solution even when one is available because it tries the goals in a fixed order. It can happen that achieving goal A consumes one of the preconditions for achieving goal B, thus blocking a solution that would be found if goal B were attempted first. Add backtracking to the GPS so that when one approach fails, GPS backs up and tries to achieve a different goal first.


created June 18, 1996
last revised June 19, 1996

John David Stone (stone@math.grin.edu)