![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Recursion
The books where I learned programming from were almost all opposed to using recursion, but my computer science teacher tells us to use recursion religiously. What's your opinion on using recursion? Should recursion be used, or is it better to be avoided?
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Dec 2004
Posts: 35
Rep Power: 0
![]() |
Recursion is best used where it solves the problem with considerably less effort and without being terribly inefficient. Anyone who has a strong opinion either way is needlessly restricting himself.
|
|
|
|
|
|
#3 |
|
Programmer
|
Both recursion and iteration have their upsides and downsides. Recursion is less efficient but more versatile (and sometimes the only way to write an algorithm). Iteration on the other hand is simpler (in my opinion), sometimes faster, and usually more efficient (especially in terms of stack usage), but is much more limited in ability and scope. I generally prefer iteration, but like Tama said, don't restrict yourself by leaning to strongly one way or the other.
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose? |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Go for both. I despised it, until I learnt about binary trees.
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
Its far harder to debug a recursive algorithm, and they are more likly to lead to memory leaks and infinate loops/recursions
![]() |
|
|
|
|
|
#6 |
|
Programming Guru
![]() ![]() ![]() |
Well, I need not saw what side of the fence I am on (if you aren't sure why, check my alias). Also, recursion leads to efficiency, which leads to robustness... but some things need not be recursive... if you think the code is going into iterative overkill, you can probably swap the algorithm out with a more recursive oriented one.
btw: Infinite Recursion and missing that damn semicolon at the end of my c++ programs was very common when I was in college... and not just common for me, but the whole department... its the "nature of the beast".
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#7 | |
|
Newbie
Join Date: Nov 2004
Posts: 22
Rep Power: 0
![]() |
Quote:
![]() |
|
|
|
|
|
|
#8 |
|
Newbie
|
Go for whatever gets your problem solved the best.
__________________
On and on.... |
|
|
|
|
|
#9 |
|
Professional Programmer
|
There are some cases where a recursive algorithm isn't more efficient, but then you get to fields like Dynamic Programming etc, of which i don't know too much. Say you need to compute x primes using a recursive algorithm. Every time you goto compute the next one, you have to start from the beginning, so you're recalculating everything x times. Dynamic programming tries to eliminate that by storing calculated values when you calculate them the first time. There's my 2 cents..
Dizz |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Mar 2005
Posts: 8
Rep Power: 0
![]() |
Some languages are optimized for it. I write a lot of Lisp and I really dont use iteration that much because recursion works so well. I've found that you can usually solve a problem either way, but depending on the algorithm and the code one may be faster/more efficient than the other.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|