Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1030952]
help
Chatterbox?
[marto]: karlgoethebier no :)
[karlgoethebier]: marto: thanks
marto should develop the 'anti-social network'
[marto]: sign up to make it impossible to be contacted online :P

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2017-12-14 11:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (389 votes). Check out past polls.

    Notices?