http://www.perlmonks.org?node_id=257846


in reply to Iterative vs Recursive Processes

I may be missing some deeper nested (no pun intended :) point, but how is your factorial_iterative() interative? Inside you simply make a call to fi_helper() which is a recursive subroutine.

An interative routine would be the one which didn't make a call to itself and instead used loops to complete the task (as some of other commenters already did).

(ps: the subject of this thread is great and I enjoyed reading both your and other monks' posts :)

update: mvaline, ahh that makes sense now ;-) Thanks for your reply.

_____________________
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce
the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true."

Robert Wilensky, University of California

Replies are listed 'Best First'.
Re: Re: Iterative vs Recursive Processes
by mvaline (Friar) on May 15, 2003 at 16:14 UTC

    It is a bit tricky. The point I made in my post is that there is (or at least should) a difference between a recursive procedure and a recursive process. The term recursive procedure only refers to the fact that the procedure definition refers to itself. This doesn't necessarily result in a recursive process.

    The difference between the processes is whether the process is characterized by linear growth because of delayed stack operations (recursive) or whether the process is static because the state of the program is defined entirely by state variables.

    fi_helper() is indeed a recursive procedure, but written in a language that supports tail-recursion (like LISP), it doesn't generate a recursive process. My question was whether it would generate a recursive process in Perl.

      I see - you refer to the source as describing the procedure. So in this case a given procedure can within its definition use itself and thus become a recursive procedure. Moving onto "process", you're referring to the state accrued through execution of a process. You're stating that a process is recursive if it maintains its stack (sort of). You can have perl throw away the current context by using the goto &sub function though that isn't an optimal way to code. You're more likely to write that code as an interator if you intend to write it in a way that perl won't penalize you for.