Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting recursion into iteration - looking for easily understandable tutorial covering complicated recursion
    primarykey
    data
    text
    <p>I was trying to teach a junior co-worker how to convert recursive subroutine to iterator (she gets stack overflows), and ran into trouble trying to explain the process in an easy to understand way. <strong><em>I</em></strong> can do it, but I don't know how to explain it well to someone who doesn't.</p> <p>So, what I am looking for is a good tutorial which shows how to take a sample complicated recursive subroutine and turn it into a loop.</p> <p>Please note that the example in the tutorial should be <strong>complicated</strong>; I'm <strong>not</strong> looking for a trivial tail recursion example. To be more specific, the example should ideally show examples of:</p> <ul> <li><p>more than one recursive call within the same subroutine.</p></li> <li><p>more than one return statement within the same subroutine (including some after a recursive call and some independent of them)</p></li> <li><p>Recursive calls happening in the middle of nested control structure (e.g. <code>if ($x) { statement 0; if ($y) { statement1; statement2; $res = recursive_call(); $statement 3; optional return; }; statement_end; }</code></p></li> </ul> <p>It should be a practical tutorial with detailed code samples - the generic "well store the parameters on a stack" hand waving that most of my Googling turned up is very nice but not helpful to a beginner.</p> <p>Any non-functional language will do (C++, Java, Perl, pseudocode, whatever), although the specific code in question is in Perl.</p> <p><strong>The part that REALLY makes it hard to explain intelligently is of course multiple calls inside control structures - you basically need to re-enter them into the correct point when popping the stack.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload