I'd like to start off by saying this is homework; so I'm not asking for a solution, just some tips. I've been ruminating on this for about a week now. Every solution I come up with doesn't do it recursively, seeing as I can't manage to wrap my head around doing it recursively with the list as the only parameter. My professor said they were able to do it with about 6 helper functions.
As mentioned, I have to solve the problem taking taking the list as the only parameter. Here's an example of what I've got on my latest attempt.
(define (sumSublists lst)
(if (= (length lst) 0)
0
(+ (length (car lst)) (sumSublists (cdr lst)))
)
)
(define (hanoi towersNum)
(sub1 (sumSublists towersNum))
(if (= (sumSublists towersNum) 1)
(print towersNum)
(begin (if (= (sumSublists towersNum) 1)
(print towersNum)
(hanoi '((car towersNum) (cddr towersNum) (cadr towersNum)))
)
(if (= (sub1 (sumSublists towersNum)) 1)
(print towersNum)
(hanoi '((car towersNum) (cadr towersNum) (cddr towersNum)))
)
(if (= (sub1 (sumSublists towersNum)) 1)
(print towersNum)
(hanoi '((cddr towersNum) (cadr towersNum) (car towersNum)))
)
)
)
)
sumSublists returns how many disks there are in the game. I'm getting an infinite loop since every time it gets used it takes the same value, thus not ever reducing the value. My problem with that, is I'm so used to imperative and OOP with the use of variables that I'm not sure how I can do this when Hanoi only takes one parameter.
The code for Hanoi is my attempt to turn my project from C++ into Scheme.
Any help is appreciated. I'm using Dr. Racket as my IDE by the way.
If you know how to solve it with the three arguments holding the three lists
A
,B
andC
, you already know how to solve it with the one argument holding the three lists in a list,(A B C)
.The problem is counting, as you're not allowed to use a separate argument to hold the number in it. Never mind that, you can count by making a list, its length to serve as the number. And if a list's length is
n
, it is easy to make a list with lengthn-1
from it. So in the end all you need isnull?
.This scheme would have you always move items from the first entry on the argument list to the second, using the head element as temporary, where you'd open and close new "levels" as needed. The three "poles" would have to be named, to be sorted back into the original order for the final return.
(cf. another answer of mine which discusses the actual recursive algorithm itself).
Tower of Hanoi is an inherently recursive problem and it has an elegant recursive solution. I won't give you the solution outright, but I'll explain the intuition behind the solution. Consider:
Given
n
discs, the smallest disc is labeled1
and the largest disc is labeledn
. Our job is to move all the discs from stackA
to stackC
while maintaining the invariant that a bigger disc may never be placed on top of a smaller disc. Without this invariant, the solution would be trivial.Now, the only way we can move disc
n
fromA
toC
is by first moving all the discs above it fromA
toB
.Our solution is now simple. We can solve our problem in three simple steps. First, we move all the discs above disc
n
fromA
toB
. Next, we move discn
fromA
toC
. Finally, we move the remaining discs fromB
toC
.Our problem of moving all the discs from
A
toC
has now been divided into two subproblems:n
fromA
toB
.n
fromB
toC
.How do we solve these subproblems? We don't have to. By the principle of recursion, we have already solved these subproblems.
Now, we have three stacks labeled
A
,B
andC
. However, we want to talk about these stacks in terms of source, destination and auxiliary stacks. When solving the problem of moving discn
fromA
toC
, we callA
the source andC
the destination. In addition, we need to make use ofB
which we call the auxiliary stack.An important thing to notice is that the source, destination and auxiliary stacks keep on changing. For the problem “moving disc
n
fromA
toC
” the source wasA
and the destination wasC
. However, for the problem “moving discn - 1
fromA
toB
” the source isA
but the destination isB
. The third stack (not source nor destination) is always the auxiliary stack.This should give you enough insight to solve the Tower of Hanoi problem. When you do solve it you'll be surprised to find out how simple, elegant and straightforward the solution is compared to imperative code.