[racket] Missionaries and cannibals
The exercise can be found at:http://htdp.org/2003-09-26/Book/curriculum-Z-H-40.html#node_sec_32.2
I am stuck at 32.2.6-.7
I originally had some information incorrect and was able to get mc-solution working correctly, except it didn't need someone to remain on the boat at all times. So it just moved 1m 1c, than 2m, and finally 2c. I found out why this happened and edited the possible-move function to take into account the boat position before making calculations. This worked great but now I am having trouble getting mc-solution working correctly. I didn't do it exactly as the book wanted, instead I had a helper function which determined if some state could reach a final state, if it could, it produces true. To get mc-solutions to produce the list of moves to final I used the first function and tested it at every step, if it returned true, I added the state to an accumulator. When I reached a final state I returned the accumulator.
I am having trouble creating the first function right now, I know if I can figure it out I can easily finish.
1.My thought is to test the input state, if it's final, return true.
2.else, locally define next-moves which is a list of legal possible moves.
3.Test if (first next-moves) is in the accumulator, if it is apply to rest next-moves.
4.Test if next-moves is empty, if it is return false.
5.recur on first in next-moves, add input state to accumulator.
6. recur on rest in next-moves.
The problems I have faced is, it is searching a list of listof states, and when it applies final on rest next-moves, the function which tests final throws an error. I was able to fix this, although, probably not correctly, but this "solution" caused an infinite loop. I need some help here. If you would like to see the code I have just let me know.
For the proper way I should have gone which Im also playing around with, I am slightly confused by the wording in the book.
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
It simply wants true or false, and not a list to false, right?
Im thinking that the goal is to define mc-solvable? and use it in mc-solution sort of like the backtracking algorithm for finding-route.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20101129/e6cbd6eb/attachment.html>