Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Tail-recursion in perl?!

by pg (Canon)
on Jul 09, 2005 at 19:28 UTC ( [id://473703]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found