Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Unrolling recursion

by dragonchild (Archbishop)
on Mar 13, 2002 at 19:10 UTC ( [id://151492]=perlmeditation: print w/replies, xml ) Need Help??

This may be more of a question for SOPW, but I'm curious as to how people unroll recursive subroutines, especially those with a while- or for(each)-loop within it.

Bonus points for someone who can use Perlisms as short-cuts to do stuff other languages can't. :-)

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Replies are listed 'Best First'.
Re: Unrolling recursion
by clintp (Curate) on Mar 13, 2002 at 22:50 UTC
    One general technique is to use a stack:
    seed the stack with the first instance while (stack has entries) pop off top entry from stack process it take any potential new work as a result of processing, put it back on the stack
    And depth/breadth first recursion can be adjusted by shifting instead of popping at the top of the loop (or when inserting new work).

    Of course this only replaces the subroutine stack with your own, but it can be quicker and save memory. (Sometimes it's a lot more obvious too!)

      From a /msg request for dragonchild, here's pseudocode to do a directory traversal without recursion.
      # Traverse the filesystem push to stack / while currentdirectory = pop stack open the currentdirectory foreach pathname in this directory if it's also a directory, push it (full path) on the stack close the current directory
      As opposed to a recursive solution:
      call traverse(/) function traverse (currentdirectory) open currentdirectory foreach pathname in this directory if it's also a directory, traverse(pathname) close currentdirectory
        For those who don't grok pseudocode here's a perl translation for directory traversal without recursion
        my @stack = "."; while(my $cd = pop @stack) { opendir(my $dh, $cd) or die("bad dir - $!"); foreach (readdir($dh)) { next if /^\./; print $_.$/; push @stack, "$cd/$_" if -d "$cd/$_"; } }
        and with recursion
        &traverse("."); sub traverse { my $dir = shift; opendir(my $dh, $dir) or die("bad dir - $!"); foreach (readdir($dh)) { next if /^\./; print $_.$/; &traverse("$dir/$_") if -d "$dir/$_"; } }
        That's a pretty neat 'trick' which I guess might be how optimising compilers implement some forms of recursion (or something).
        HTH

        broquaint

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://151492]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-24 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found