A good introduction to recursion is Higher-Order Perl Chapter 1: Recursion and Callbacks. The walk-through starts simple with binary numbers, then moves on to directory traversal, the meat and potatoes of recursion :) A quick foray into html tree traversal (just like directory tree, trees are the meat and potatoes ) and it ends with Fibonacci in section 1.8 When Recursion Blows Up

Devel::TraceCalls shows the tree (book one or two nice pics)

#!/usr/bin/perl -- use strict; use warnings; use Devel::TraceCalls { Subs => [qw/fib /]}; print fib(5); sub fib { my $n = shift; if ($n < 2) { return $n } fib($n-2) + fib($n-1); } __END__ TRACE: main::fib( 5 ) TRACE: +-main::fib( 3 ) TRACE: | +-main::fib( 1 ) TRACE: | +-main::fib( 2 ) TRACE: | ! +-main::fib( 0 ) TRACE: | ! +-main::fib( 1 ) TRACE: +-main::fib( 4 ) TRACE: | +-main::fib( 2 ) TRACE: | ! +-main::fib( 0 ) TRACE: | ! +-main::fib( 1 ) TRACE: | +-main::fib( 3 ) TRACE: | ! +-main::fib( 1 ) TRACE: | ! +-main::fib( 2 ) TRACE: | ! | +-main::fib( 0 ) TRACE: | ! | +-main::fib( 1 ) 5

If you made a directory mimicking fib-5 you can see the tree in your file browser

md fib-5 md fib-5\fib-3 md fib-5\fib-3\fib-1 md fib-5\fib-3\fib-2 md fib-5\fib-3\fib-2\fib-0 md fib-5\fib-3\fib-2\fib-1 md fib-5\fib-4 md fib-5\fib-4\fib-2 md fib-5\fib-4\fib-2\fib-0 md fib-5\fib-4\fib-2\fib-1 md fib-5\fib-4\fib-3 md fib-5\fib-4\fib-3\fib-1 md fib-5\fib-4\fib-3\fib-2 md fib-5\fib-4\fib-3\fib-2\fib-0 md fib-5\fib-4\fib-3\fib-2\fib-1

Or with tree

fib-5 |-- fib-3 | |-- fib-1 | `-- fib-2 | |-- fib-0 | `-- fib-1 `-- fib-4 |-- fib-2 | |-- fib-0 | `-- fib-1 `-- fib-3 |-- fib-1 `-- fib-2 |-- fib-0 `-- fib-1

Chapter 5: From Recursion to Iterators, deals with Eliminating Recursion from fib ( see fib-0, fib-1, fib-2 ... )


In reply to Re^2: Visualizing recursion with the fib() example by Anonymous Monk
in thread Visualizing recursion with the fib() example by stevieb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.