473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Question: How do you feel about recursion?

Greetings.

I want to get everyone's opinion on the use of recursion.

We covered it in class tonight and I want a good solid answer from
people in the "know" on how well recursion is accepted in modern day
applications.

Should we readily use it when we can or only when absolutly forced to
use it?

I appreciate any responses. Just wanted to get a feel for it. I
received a pretty dirty opinion of it tonight in class.
Jul 19 '05 #1
12 2727
da Vinci <bl***@blank.com> wrote in message
news:um********************************@4ax.com...
Greetings.

I want to get everyone's opinion on the use of recursion.

We covered it in class tonight and I want a good solid answer from
people in the "know" on how well recursion is accepted in modern day
applications.
Anything that does a good job is acceptable. Recursion is no exception. You
can't generalize about such things.
Should we readily use it when we can or only when absolutly forced to
use it?
Use it if it's the best solution in the circumstances (where "best" can mean
quickest or easiest to implement, easiest to understand, easiest to
maintain, fastest to execute, etc. etc., whatever matters to you the most).
I appreciate any responses. Just wanted to get a feel for it. I
received a pretty dirty opinion of it tonight in class.


To do something like searching for a file on a system with a nested
directory structure, I would certainly do it with recursion because it's
quick and easy to write and very effective. For something like a factorial
calculation, which can be done recursively, a simple loop is probably much
more effective. It really depends on what problem you want to solve.

DW

Jul 19 '05 #2
da Vinci <bl***@blank.com> wrote in
news:um********************************@4ax.com:
Greetings.

I want to get everyone's opinion on the use of recursion.

We covered it in class tonight and I want a good solid answer from
people in the "know" on how well recursion is accepted in modern day
applications.

Should we readily use it when we can or only when absolutly forced to
use it?

I appreciate any responses. Just wanted to get a feel for it. I
received a pretty dirty opinion of it tonight in class.


In some languages, recursion, especially tail recursion, is more natural
than in others. In Lisp, for example, I think you would use recursion to
implement what in other languages would be a simple loop.

In a language like C++ you would normally use recursion only when an
iterative solution would require implementing an explicit stack to store
the algorithmic state. Since a stack is required anyway, might as well
use the call stack if it simplifies the implementation. Traversing a tree
structure would be an example of something best implemented recursively.
The only caveat is the limited stack space some environments impose.

Computational algorithms that are expressed as recurrence relations, such
as Fibonocci sequence or factorial, are generally better implemented
iteratively, since such an implementation does not require a stack, and
so is simpler and faster done interatively.

Gregg
Jul 19 '05 #3
da Vinci wrote:
Greetings.

I want to get everyone's opinion on the use of recursion.

We covered it in class tonight and I want a good solid answer from
people in the "know" on how well recursion is accepted in modern day
applications.

Should we readily use it when we can or only when absolutly forced to
use it?

I appreciate any responses. Just wanted to get a feel for it. I
received a pretty dirty opinion of it tonight in class.


When a recursive solution is natural for a given problem -- and you
know it is well bounded (i.e. will not get too deep) -- use it. If
it proves to be a bottleneck (as discovered through profiling) you
can always rewrite it as iteration. Some C++ compilers, in
optimizing mode, can even transform some tail recursive algorithms
into iteration by themselves.

In any event, *understand* recursion. You may not use it in your
actual code but it will quite likely inform your programming -- and
improve it.

That said, you're really off-topic here; please take your question
to news:comp.programming. [f'ups set]

HTH,
--ag
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 19 '05 #4
da Vinci wrote:
...
I want to get everyone's opinion on the use of recursion.

We covered it in class tonight and I want a good solid answer from
people in the "know" on how well recursion is accepted in modern day
applications.

Should we readily use it when we can or only when absolutly forced to
use it?

I appreciate any responses. Just wanted to get a feel for it. I
received a pretty dirty opinion of it tonight in class.


The term "recursion" has many different meanings, depending on the
context. There is no point to start a debate about "recursion" without
providing a clear definition for the term. What kind of recursion your
question is about? Syntactical recursion, when one function in C or C++
program calls itself directly or indirectly? Algorithmical recursion,
which implements stack-based version of 'divide & conquer' approach?
Something else?

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #5
Andrey Tarasevich writes:
da Vinci wrote:
...
I want to get everyone's opinion on the use of recursion.

We covered it in class tonight and I want a good solid answer from
people in the "know" on how well recursion is accepted in modern day
applications.

Should we readily use it when we can or only when absolutly forced to
use it?

I appreciate any responses. Just wanted to get a feel for it. I
received a pretty dirty opinion of it tonight in class.


The term "recursion" has many different meanings, depending on the
context. There is no point to start a debate about "recursion" without
providing a clear definition for the term. What kind of recursion your
question is about? Syntactical recursion, when one function in C or C++
program calls itself directly or indirectly? Algorithmical recursion,
which implements stack-based version of 'divide & conquer' approach?
Something else?


Jeez.

Use recursion when it's natural. If its use strikes you as forced (as in
factorial), it probably is. I would not want to code a tree or fiddle with
the DOS file structure if I had a language that did not support recursion.
The original languages, Cobol, Fortran and Basic could not "do" recursion.
Your instructor might be from that era.
Jul 19 '05 #6

"da Vinci" <bl***@blank.com> wrote in message
news:um********************************@4ax.com...
Greetings.

I want to get everyone's opinion on the use of recursion.

We covered it in class tonight and I want a good solid answer from
people in the "know" on how well recursion is accepted in modern day
applications.

Should we readily use it when we can or only when absolutly forced to
use it?

I appreciate any responses. Just wanted to get a feel for it. I
received a pretty dirty opinion of it tonight in class.


It's good for learning and making you think of new techniques. It does make
certain problems much simpler, but in fact in general programming it's not
often used. (I didn't say never.)
Jul 19 '05 #7
da Vinci wrote:
Greetings.

I want to get everyone's opinion on the use of recursion.

We covered it in class tonight and I want a good solid answer from
people in the "know" on how well recursion is accepted in modern day
applications.

Should we readily use it when we can or only when absolutly forced to
use it?

I appreciate any responses. Just wanted to get a feel for it. I
received a pretty dirty opinion of it tonight in class.


You should recurse when you are doing something that naturally lends to
it (seaching a tree, the main example you will come across in normal
life is searching a hard disc).

The overheard that comes from recursion unless you are doing 10,000s of
levels is tiny, and almost certainly smaller than the improvement you
would get with algorithms improvements. Also there is a good chance your
attempts to avoid recursion will be less efficent than just doing it
(this applies more and more with modern compilers)
Jul 19 '05 #8
In article <um********************************@4ax.com>, bl***@blank.com
says...

[ ... ]
I want to get everyone's opinion on the use of recursion.
That's a bit like asking "I want people's opinions of large trucks."
It's so vague as to be nearly meaningless.
Should we readily use it when we can or only when absolutly forced to
use it?


IMO, you should readily use it when doing so will lead to code that's
easier to read and/or write than doing otherwise. Using it to create
simple loops is pointless except in languages (e.g. LISP) that don't
really support loops directly, or in situations (e.g. template meta-
programming) where the language does, but not under the circumstances
you care about.

OTOH, there are also times that recursion seems to be a natural fit, but
ends up working relatively poorly. Elsethread, traversing a tree-like
file system has been mentioned, so I'll use it as well. The problem
here is that traversing a tree-like file system recursively naturally
tends to lead to a depth-first traversal, but this tends to make
exceptionally poor use of caching. At least with all the OSes I've
programmed, if you care much about speed, you're almost always better
off doing a breadth-first traversal instead.

This way, you read an entire directory at once, and then go to the next
directory. This can avoid quite a bit of disk seeking as well as
improving cache usage. The difference depends a lot on the directory
structure, but if (for example) you were searching a large disk, a 10:1
speed improvement wouldn't be surprising at all.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #9
osmium wrote:
...
The term "recursion" has many different meanings, depending on the
context. There is no point to start a debate about "recursion" without
providing a clear definition for the term. What kind of recursion your
question is about? Syntactical recursion, when one function in C or C++
program calls itself directly or indirectly? Algorithmical recursion,
which implements stack-based version of 'divide & conquer' approach?
Something else?

...
Use recursion when it's natural. If its use strikes you as forced (as in
factorial), it probably is. I would not want to code a tree or fiddle with
the DOS file structure if I had a language that did not support recursion.
The original languages, Cobol, Fortran and Basic could not "do" recursion.
...


Once again, the correctness of these statements greatly depends on the
concrete meaning of the term "recursion" (and believe me, it has more
than one). This has been discussed to death in more appropriate places.
I don't see any reason to do it again here.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #10
On Tue, 28 Oct 2003 10:02:52 -0800, Andrey Tarasevich
<an**************@hotmail.com> wrote:
Once again, the correctness of these statements greatly depends on the
concrete meaning of the term "recursion" (and believe me, it has more
than one). This has been discussed to death in more appropriate places.
I don't see any reason to do it again here.


Then lets not.

I just learned it last night and with everything, I wanted the "pros"
opinion on it. I wasn't aware there was more than one type as *I just
learned it*. I was refering mainly to the fact of a function calling
itself.

I have made a few posts in the past regarding different issues and
have been told by many people on the group that the way I was doing
something was not accepted in the real world. I want to learn what IS
acceptable.

As I said, quite a few things I have learned already are actually NOT
acceptable in the real world. So, I have curtailed my use of those
practices on the advisement of group members. (The big one being that
I was told that using the header call of #include <whatever.h> was
perfectly fine to use in C++ and group members advised me otherwise.)

So, this thread was begun with the intent to see if recursion is
generally accepted or if it is shunned.

I was unaware there are multiple meanings to recursion, other than
just a function calling itself.
Jul 19 '05 #11
da Vinci wrote:

Greetings.

I want to get everyone's opinion on the use of recursion.


It makes me feel good.
And THAT makes me feel good.
Which makes me feel good.
<recurse ad nauseum>

:)
Jul 19 '05 #12
In article <um********************************@4ax.com>,
da Vinci <bl***@blank.com> wrote:
Greetings.

I want to get everyone's opinion on the use of recursion.


The answers will probably be the same as if you asked everyone's
opinions on linked lists.

Recursion is a tool. It can be an obvious and good choice, an obvious and
bad choice (using it to compute the nth Fibonacci number is an example of
that), and one that is completely inapplicable to the problem.

If it isn't in your bag of tricks you will be the poorer for it. If you
use it everywhere it can be used then you will undoubtedly use it in places
where it shouldn't be used.

Alan
--
Defendit numerus
Jul 19 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: MetalOne | last post by:
I am trying to write a generator function that yields the index position of each set bit in a mask. e.g. for x in bitIndexGenerator(0x16): #10110 print x --> 1 2 4 This is what I have, but...
2
by: JP SIngh | last post by:
Hi All Please can someone help me solve this issue. I have a database table the structure of which are given below. Profile Table EmployeeNumber FirstName
9
by: JustSomeGuy | last post by:
I have a class that looks something like this (Don't try to compile it, I haven't tested this) class KVP { string key; string value; list<KVP> sublist; };
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
4
by: Daniel | last post by:
I need to build the maze board(like 2d array) using a tree. But I find that my buildTree function doesn't work. Could anyone give me some hints on debugging it? Thanks bool BuildTree(TreeNodePtr...
4
by: Michael Thomas Cassady | last post by:
I know my subject wasn't the best but I hope people still look at this. I know that this is probably compiler and implementation dependent and that this is a standard c discussion group but can...
16
by: Alvin Bruney | last post by:
I'm observing that a sleeping thread changes to stopped after a while. Is that accepted framework behavior for web applications? My thread basically does some work, and sleeps for 60 minutes...
27
by: Chad | last post by:
The problem is: Write a recursive version of the function reverse(s), which reverses the string s in place. In "The C Answer Book", Second Edition, near the bottom of page 95, the authors say...
30
by: gaoxtwarrior | last post by:
a sort which is stable means it keeps the object's original order. A sort which is in place is stable. does anyone can explain me what is sort in place? thx.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.