wow.. vey good point.. thanks
Laurent_R.
The central point, as many times, is a correct understanding of words and semantic they cover.
This can be useful for the OP and for other as me that have missed this difference (from wikipedia):
Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem
Iteration is the repetition of a block of statements within a computer program
That said obviously the tachyon solution falls in the iterative category..
#excert from tachyon's code at http://www.perlmonks.org/?node_id=15785
+1
my @dirs = ($root);
for my $path (@dirs){
opendir ( DIR, $path ) or next; # skip dirs we can't read
while (my $file = readdir DIR) {
next if $file eq '.' or $file eq '..'; # skip dot files
if ( -d $path.$file ) {
push @dirs, $path.$file.'/'; # add dir to list
}
}
closedir DIR;
}
but..
..the logic of the snippet IS recursive in the way it populates
@dirs: in fact it iterates over a list updated dynamically while processing the iteration itself. It produes a directory tree, or well a list of different depth objects.
Also here the problem is divided in small pieces to process. But in the opposite way:
@dirs starts populated by the root node only and while it is processed the stack
@dirs itself it is updated: the way
@dirs is populated by
push seems recursive, in a broad sense.
In other words, as
wikipedia tell us some line below:
Recursion and iteration are equally expressive: recursion can be replaced by iteration with an explicit stack, while iteration can be replaced with tail recursion. Hth
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.