3 ms·
> Is recursion really worth it? It is a useful tool. Knowing how to do recursion is better than not knowing. > My CS teacher told me to avoid recursion when
by pwg 4y ago
> Is recursion really worth it?
It is a useful tool. Knowing how to do recursion is better than not knowing.
> My CS teacher told me to avoid recursion when possible.
Bad CS teacher. There are some algorithms that are much more elegant and easier to build in a recursive manner (i.e., walking a tree being one).
> Apparently recursion takes up more memory space than nested for loops (I'm talking about 4-5 for loops) and I lose grades for the same.
That really depends upon what local variables the recursive routine allocates. But, unless you are studying for building embedded systems with seriously constrained memory space, the difference in memory is negligible in today's typical systems with gigabytes available. And an easier to understand algorithm in recursive form would outweigh a difference in memory usage anyway.
> I lose grades for the same.
Again, bad CS teacher. Unless the problem statement explicitly indicated to not use a recursive routine, a recursive solution should be graded on whether it fulfills the rest of the problem statement requirements, not on the mere fact that it is recursive.
- dieselgate 4y agoThought you were going to say “knowing when Not to use recursion” is important. It’s a handy concept and tool but not practical/relevant for a lot of real world/day to day stuff. And of course there’s the obligatory joke: What’s the B in Benoit B Mandelbrot stand for? Benoit B Mandelbrot ;)
- verdverm 4y ago> Bad CS teacher Most definitely. OP should get into a different class or university. I'd be concerned about the program generally if someone is teaching these ideas
- lahvak 4y ago> Bad CS teacher. You don't know. Maybe she wants them to recognize and use folds whenever possible, rather than using low level recursion all the time. You know what they say: "Someone has to write recursive functions, but it does not have to be you!"
- 082349872349872 4y agoCS departments come in two flavours: systems and theory. The more dogmatic of each both eschew recursion, but have motivations that are diametrically opposed. :)
- mrnonchalant 4y agoWhat are the reasons?
- 082349872349872 4y agoAs given above: in a dogmatic systems department, recursion is to be avoided because its naive implementations use more machine resources than their equivalent loops; in a dogmatic theory department recursion is to be avoided because it is more difficult to prove properties of potentially unstructured recursions than of structured folds. The dogmatic systems department wishes to replace recursion with something more concrete; the dogmatic theory department with something more abstract.
- kerpotgh 4y ago[dead]