Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Recursion Confusion

by Anonymous Monk
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.


Comment on Re: Recursion Confusion
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1030952]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2015-07-06 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (84 votes), past polls