Perl Monk, Perl Meditation PerlMonks

### Re: Recursion Confusion

 on Apr 27, 2013 at 11:04 UTC ( #1030952=note: print w/replies, xml ) Need Help??

in reply to Recursion Confusion

A tip from Visualizing recursion with the fib() example is to use Devel::TraceCalls
```#!/usr/bin/perl --
use strict; use warnings;
use Devel::TraceCalls { Subs => [qw/ hanoi /]};
hanoi( 3, 'A', 'C', 'B', 0 );
sub hanoi
{
my (\$n, \$start, \$end, \$extra) = @_;
if (\$n == 1)
{
print "Move disk #1 from \$start to \$end.\n";
}
else
{
hanoi(\$n-1, \$start, \$extra, \$end);
print "Move disk #\$n from \$start to \$end.\n";
hanoi(\$n-1, \$extra, \$end, \$start);
}
}
__END__
```TRACE: main::hanoi( 3, 'A', 'C', 'B', 0 )
TRACE: +-main::hanoi( 2, 'A', 'B', 'C' )
TRACE: | +-main::hanoi( 1, 'A', 'C', 'B' )
Move disk #1 from A to C.
Move disk #2 from A to B.
TRACE: | +-main::hanoi( 1, 'C', 'B', 'A' )
Move disk #1 from C to B.
Move disk #3 from A to C.
TRACE: +-main::hanoi( 2, 'B', 'C', 'A' )
TRACE: | +-main::hanoi( 1, 'B', 'A', 'C' )
Move disk #1 from B to A.
Move disk #2 from B to C.
TRACE: | +-main::hanoi( 1, 'A', 'C', 'B' )
Move disk #1 from A to C.

Create A New User
Node Status?
node history
Node Type: note [id://1030952]
help
Chatterbox?
 [Eily]: (while the same for @*, @(, and @) throw a warning) [talexb]: TStanley Cool. Yeah, that's about 45 minutes away. Nice spot.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2017-06-23 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (548 votes). Check out past polls.