4 ms·
Thanks, this is very interesting. I am enrolled in a CS Bsc. (night courses) and while we learned about the different techniques of recursive programming, we ne
by valesco 8y ago
Thanks, this is very interesting. I am enrolled in a CS Bsc. (night courses) and while we learned about the different techniques of recursive programming, we never learned how to convert a recursive solution into an iterative one.
- iliketosleep 8y agoI remember when I was at college it was the same, and it doesn't make any sense. I can't recall any instances where I've needed to convert an iterative solution into a resursive one, but there have been countless times I've done the reverse. I belive that the mindless emphasis on resursive programming in college courses has lead to poor practices, where people are using recursion to just make their code more elegant where an iterative solution wouldn't much more complicated but a lot more robust. The result? Down the track, an unforseen use cases resulting in max stack size exceeded errors, causing needless headaches.
- saagarjha 8y ago> I belive that the mindless emphasis on resursive programming in college courses has lead to poor practices, where people are using recursion to just make their code more elegant Most courses, especially introductory ones, don’t have particularly good examples of when to use recursion. This doesn’t mean that there aren’t cases where recursion isn’t useful, though.
- gh02t 8y agoI think the prevalence of recursion might be attributable to mathematics. A lot of mathematical analysis of algorithms is more natural to express in terms of recursion (partially because of proof by induction), so you often see algorithms stated that way. It's also worth noting that there are plenty of instances where (IMO), recursion is much more natural. For example, traversing trees recursively. I agree, though, there should definitely be more time spent teaching how to convert between the two.
- ericlippert 8y agoOne of the reasons I wrote this article was because of exactly the problem that you mention, so I am very happy that you found it interesting! I frequently interview recent CS graduates, and ask a technical question about techniques for removing recursions; the people who do well on such questions tend to have practical experience writing compilers. A lot of these basic techniques are not covered in undergraduate courses, but they are crucial to understanding how modern programming languages work under the hood. I still have my notes from my fourth year course in continuation passing style from my undergrad days and it was very helpful to me when we started adding these concepts to C#.