Recursive Python Considered Harmful
ceasarjames.wordpress.com
ceasarjames.wordpress.com
It is very unfortunate that many programmers (including Guildo, circa 2009) think this way. Guildo in particular, when he talked about recursion, his state of mind is about "cons" and "tail" of Scheme.
On the other hand, it is undeniable that algorithmic thinking and problem solving skills are critical to being a good programmer. And as of today those who are trained in the tradition of the Computer Science discipline are equipped with an array of algorithmic strategies all based on recursion. Techniques such as divide and conquer, dynamic programming and much more are all based on the idea of solving a problem in terms of itself, i.e. recursion.
When a programming language eschew recursive strategies, it inadvertently wastes precious skills that are taught to programmers.
Recursions are hard, but they are very powerful, when used properly. There are many problems that can be solved easily and faster with recursion, and you can't simply convert them to loops.
I agree with that last part, but I can't imagine thinking of every algorithm iteratively. Binary search, merge sort, and pretty much any tree algorithm are examples of algorithms that (to me) are much less natural to thing of iteratively.
Tradeoffs like that are what highly dynamic languages are all about. You shouldn't be using Python (yet) if the natural expression of your algorithm is too expensive for your problem size (with today's implementations). Making your Python non-idiomatic and hard to read will cost you its advantages, and won't even buy you much time before you find yourself porting to Java or something similarly optimized.
He didn't say that, so thankfully there's nothing to disagree with.
But I'm under the impression that this is seldom (if ever) the case with recursive solutions that can benefit from tail call elimination... In this case, an iterative solution is just as simple (or complicated). It is a matter of taste.
When this is not the case, you can't perform this optimization anyway. Am I wrong here?
I think that's only true if the function makes only one recursive call, like computing the Fibonacci numbers. If the function calls itself recursively twice, it may end up using a lot of memory, e.g. the Ackermann function.