There's more than one way to do things | |
PerlMonks |
Re^3: cannot follow hanoi subroutineby GrandFather (Saint) |
on Nov 05, 2007 at 09:15 UTC ( [id://648977]=note: print w/replies, xml ) | Need Help?? |
I suspect you are having trouble coming to grips with recursion. Recursion happens when a sub calls itself (either directly or indirectly). It is a powerful and elegant way of solving problems and works by "remembering" some state information on the subroutine call stack. You will notice that the hanoi sub calls itself in two places - that's where the recursion happens. Notice that each time hanoi calls itself it does so with the first parameter set to one less that the value on entry thus the recursion eventually stops with $n ==1. The following version that in essence dumps the call stack on each entry to hanoi and reports each exit from the sub may make it a little clearer (the entry numbers are the lines that hanoi was called from):
Prints:
Perl is environmentally friendly - it saves trees
In Section
Seekers of Perl Wisdom
|
|