Re: Any use for recursion?
- From: Ed Prochak <edprochak@xxxxxxxxx>
- Date: Wed, 4 Jun 2008 04:51:51 -0700 (PDT)
On Jun 2, 9:59 pm, Tomás Ó hÉilidhe <t...@xxxxxxxxxxx> wrote:
There's quite a few algorithms that can be implemented as a[]
recursive function. Of all the ones I've seen though, it's been
trivial to re-write them as a normal loop. An example would be
printing an integer value as binary with the MSB first and without
leading zeroes. The following is written in plain old C.
Can someone please post an example, if any, of an algorithm that's
better done as a recursive function? Of course when I say, "better",
it could refer to things like:
* Short (e.g. just three lines of code)
* Easy to understand when you first glance at it
* Fast execution time
* Minimal resource requirements (e.g. memory and stack space)
but I'd like to hear what arguments people would pose in support of
using recursive functions? Regarding the example I give above, well I
think it's nice and short but then I don't think this justifies the
ridiculous overhead in terms of execution speed and stack usage.
(Please ignore the fact that the code calls "putchar" which has
massive overhead thus making negligible the overhead imposed by the
recursion, I can give other examples -- or at the very least pretend
that putchar is routed into a communications link that's running at 1
gigabit per second).
Basically I'm asking: Are recursive functions for fun and nothing more?
A recursive parser.
try writing that in a loop and making it easy to understand.
Actually in the microcontroller environment you may be better off
leaning toward the loop versions of such algorithms. But on PC and
higher class machines the ease of implementation can justify the
recursive versions. It's back to the basic engineering principle: use
the right tool for the job.
Ed
.
- References:
- Any use for recursion?
- From: Tomás Ó hÉilidhe
- Any use for recursion?
- Prev by Date: Re: Any use for recursion?
- Next by Date: Re: How to (gu)estimate a software project duration?
- Previous by thread: Re: Any use for recursion?
- Next by thread: Re: Any use for recursion?
- Index(es):
Relevant Pages
|