in reply to Tail-recursion in perl?!

"Is there something similar in perl, or is it useless cause we have different ways to build iterative functions (as my first example)?"

In this sense, Perl is no different from other languages, especially c-group languages. Although not all languages have the "$val *= $_ for (1..$max);" syntax, this syntax didn't express any logic that can not be easily expressed otherwise.

Use recursion is not "deprecated", but rather unneccessary in this particular example. This recursion version can be found almost in all text books, not because it is the best way to code this particular logic, but it is probably the best example one can use to demo recursion.

Replies are listed 'Best First'.
Re: Tail-recursion in perl?!
by jonadab (Parson) on Jul 10, 2005 at 01:21 UTC
    This recursion version can be found almost in all text books, not because it is the best way to code this particular logic, but it is probably the best example one can use to demo recursion.

    A better example would be quicksort. The reason I say it's better is because it demonstrates how recursion is a more natural way to think about certain problems. With the factorial example, the iterative version is at least as easy to follow as the recursive version, but with quicksort the recursive version is *MUCH* easier to understand. Perhaps an even better example would be a depth-first traversal of a general tree.