I need to build a recursive solution to Towers of Hanoi in Scheme. This is different from many of the other Hanoi Scheme questions that have been asked on here before, because I need actually alter the list, not just return a list of instructions.
Requirements
The function call should look like:
(define towers '((1 2 3)()()))
(hanoi towers)
I should return the list every time the peg is moved.
The Status
I have two parts of the puzzle done:
Altering lists in Scheme, I do this by just building a new list using cons or list.
The logic of Towers of Hanoi. This logic is everywhere, I understand how the puzzle is supposed to work.
I'm just having trouble combining them together.
Relevant Code
(define (first-flip Tower)
(list (return-stack 1 Tower)
(return-stack 3 Tower)
(return-stack 2 Tower)))
(define (second-flip Tower)
(list (return-stack 3 Tower)
(return-stack 2 Tower)
(return-stack 1 Tower)))
(define (hanoi height Tower)
(if (= height 1)
(move Tower)
(hanoi (decrement height)
(second-flip
(move
(hanoi (decrement height) (first-flip Tower)))))))
Summary of Other Code
You can assume the following about my code:
Return stack will return an unnested list (1 source, 2 destination, 3 auxiliary/temp).
So (return-stack 1 '((1 2 3)()()))
would return '(1 2 3)
.
Move will move the top peg of the leftmost list to the top of the middle list.
So (move '((1 2 3)()()))
would return '((2 3)(1)())
.
Decrement will just return the parameter-1.
Finally, don't worry that the hanoi function listed here takes slightly different parameters than the required hanoi function, I will just create a shell with the proper parameters to call this one.
The Problem
I'm almost certain that the problem lies in my hanoi function. There is something wrong about the logic I'm calling. I've had several TA's look at the code and they were not able to offer relevant help due to a lack of knowledge of scheme and of Towers of Hanoi. I'm hoping the good citizens of the internet can help me here.