``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. ``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.''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. 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:
<#42833#>(define<#42833#> <#42834#>MC<#42834#> <#42835#>3)<#42835#> <#42836#>(define<#42836#> <#42837#>BOAT-CAPACITY<#42837#> <#42838#>2)<#42838#>Formulate the function in terms of these constants.
<#42864#>Tests<#42864#>:\ Formulate all tests as boolean-valued expressions that
produce <#67829#><#42865#>true<#42865#><#67829#> if the expected value is the computed one, and
<#67830#><#42866#>false<#42866#><#67830#> if not.~<#67831#><#67831#>
<#42869#>Exercise 32.2.3<#42869#>
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.
<#42894#>Exercise 32.2.6<#42894#>
<#42917#>Note<#42917#>:\ This shows how the accumulator inside the state representation has two uses.~ Solution<#67843#><#67843#>