[Israel.pm] Understanding recursion [was What does this code do?]
Shlomi Fish
shlomif at iglu.org.il
Sat May 8 03:20:38 PDT 2004
On Monday 03 May 2004 12:07, Srikanth Madani wrote:
> Shlomi, thanks for the illuminating reply.
>
You're welcome.
> >Now, when to use recursion? Most of the tail-recursion and
> > linear-recursion can be implemented as loops. Still, recursion is
> > sometimes used to clearly illustrate the algorithm, make the code
> > clearer, or calculate the complexity of the algorithm.
>
> I don't see how recursion makes the code clearer.
> When I have to re-engineer code, or investigate a subtle-bug, one of the
> things I do is to examine data flow mentally or on paper. This just doesn't
> work with recursion - my mind has a stack depth of three or four!
>
Well recursion is the same process. You need to think of it as the same
function calling itself again to perform a certain task. And it somehow
collects all the steps in between or integrates them into something whole.
> How does one start trying to understand undocumented recursive code?
>
First you need to isolate the recursion and see what it does. Identify its
type and how it fits into the rest of the function. Then, build a mental
model of the function as one integrated whole.
A good idea for you may be to read the book "Structure and Interpretation of
Computer Programs" (at least the first three chapters). This book covers a
lot of different types of processes using recursions, and you'll probably
understand it better. Its homepage (where it is available online for free
reading) is:
http://mitpress.mit.edu/sicp/
Regards,
Shlomi Fish
> Cheers,
> Srikanth Madani
> Senior Subject Matter Expert @ Amdocs Software GmbH
>
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://www.perl.org.il/mailman/listinfo/perl
--
---------------------------------------------------------------------
Shlomi Fish shlomif at iglu.org.il
Homepage: http://shlomif.il.eu.org/
Quidquid latine dictum sit, altum viditur.
[Whatever is said in Latin sounds profound.]
More information about the Perl
mailing list