On occasion, accumulators are a part of a piece of compound data because a function manages many pieces of data (in the same class) at the same time. The following story poses just such a problem:
Once upon a time, three cannibals were guiding three missionaries through a jungle. They were on their way to the nearest mission station. After some time, they arrived at a wide river, filled with deadly snakes and fish. There was no way to cross the river without a boat. Fortunately, they found a row boat with two oars after a short search. Unfortunately, the boat was too small to carry all of them. It could barely carry two people at a time. Worse, because of the river's width there was no way to bring the boat back other than to row it back.While we can solve the problem by hand, solving it with a Scheme function is more fun and more general. If the same story comes up again with different numbers of cannibals and missionaries or different boat sizes, we can use the same function to solve the problem again.Since the missionaries could not trust the cannibals they had to figure out a plan to get all six of them safely across the river. The problem was that these cannibals would kill and eat missionaries as soon as there were more cannibals than missionaries at some place. Thus our missionary-programmer had to devise a plan that guaranteed that there were never any missionaries in the minority at either side of the river. The cannibals, however, can be trusted to cooperate otherwise. Specifically, they won't abandon any potential food, just as the missionaries won't abandon any potential converts.
Luckily one of the missionaries had taken a Scheme course and knew how to solve this problem.
As with every problem, we begin by laying out how to represent the problem in our data language and then study how to represent certain actions in the real world in our programming language. Here are two basic constants concerning the data representation:
(define MC 3) (define BOAT-CAPACITY 2)Formulate the function in terms of these constants.
Exercise 32.2.1
Provide a data representation for the states of a river crossing. A
state should record the number of missionaries and cannibals on each side
of the river and the location of the boat. Here is a graphical
representation of the states:
Exercise 32.2.2
Develop a data representation for boat loads. Define BOAT-LOADS, the list of all possible boat loads.
Develop the function make-BOAT-LOADS, which consumes the boat's maximal
capacity and constructs the list of possible boat loads. Solution
One way to deal with search problems in a systematic manner is to generate all possible successor states for the states we have reached so far, to filter out the interesting ones, and to start the search over from those. A successor state is reached by using a feasible transition, for example, an enabled move in a game, a boat trip, etc.
Here is a graphical illustration of the situation for our problem:
The initial state in the top row has five possible successor states, one per feasible boat load. They are shown in the second row. Two of these successor states are illegal, because one side contains more cannibals than missionaries. One of the legal ones is the state in which one missionary and cannibal reached the right side of the river; it has three successor states in turn. The following exercises deal with generating the successor states and filtering out the interesting ones.
Testing: Formulate all tests as boolean-valued expressions that produce true if the expected value is the computed one, and false if not.
Exercise 32.2.3
Develop a function that consumes a state and returns a list of all possible successor states, that is, all those states that are reachable with one boat trip from one side of the river to the other.
Develop a generalized version that consumes a list of states, and returns a list of states reachable with one crossing. Solution
Exercise 32.2.4
Develop a function that determines whether a given state is legal. A state is legal if it contains the proper number of missionaries and cannibals and if the missionaries on each side of the river are safe.
Develop a generalized function that consumes a list of states and returns the sublist of legal states. Solution
Exercise 32.2.5
Develop a function that consumes a state and determines if it is final.
Develop a generalized version that consumes a list of states and returns
the sublist of final states. Solution
The functions we have developed can generate the successor states of a list of states and can detect whether any of the states reached so far are legal. Now we can develop a function that determines whether we can transport the missionaries and cannibals across the river.
Exercise 32.2.6
Develop mc-solvable?, which consumes a list of states and generates the list of all successor states until it has found a final state. The function should simply produce true when it finds a final state. Solution
Exercise 32.2.7
Develop mc-solution. The function is an adaptation of mc-solvable? that not only produces true when it finds a solution but a list of river crossings if a given missionary-and-cannibal problem is solvable.
Hint: Modify the state representations so that they accumulate the list of crossings that got the group to this particular state. For the initial state, this is just the empty list; for a final state, it is the desired solution. Solution
Exercise 32.2.8
A series of boat trips may bring the group of missionaries and cannibals back to the initial state (or some other previous state). The series may include two, four, or more boat trips. In short, the ``game'' contains cycles. Make up an example.
The function mc-solution generates all those states reachable with, say, seven boat trips before it generates all those states reachable with eight crossings. Therefore we do not have to worry about cycles in solution attempts. Why?
Modify the solution so that a state reached via a cycle is also illegal.
Note: This shows how the accumulator inside the state representation
has two uses. Solution