Re: Any use for recursion?



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
.



Relevant Pages

  • RE: Recursive function help
    ... You could use a single query and return all employees sorted in to levels and use nested do while loops or a recursive function. ... > Microsoft VBScript runtime error '800a01fb' ... > Function GetParents() ...
    (microsoft.public.inetserver.asp.general)
  • Re: Recursive Functions
    ... requiring it as a recursive function does scream of homework. ... >> simple for loop should be easier and faster. ... > iterative version that can hold a candle to the recursive technique? ... multiplications are possible using the square and multiply idiom. ...
    (comp.lang.c)
  • Re: Iteration in lisp
    ... In Common Lisp, always prefer a loop where practical unless you know ... recursive function can run out of stack since CL does not guarantee ... Even if you have tail recursion, you are still wasting productivity by ...
    (comp.lang.lisp)
  • Re: Iteration in lisp
    ... If the recursive function defines a iterative process, it might be as good as a loop. ... If the loop defines a linear recursive process, it might be as bad as the recursive function. ...
    (comp.lang.lisp)