I'm against iterative loops in Python simply because it seems inevitably to lead to people writing obfuscated, hard-to-read code simply for the sake of writing it imperatively, and Python is, well, not about that: it's about clear, readable code.
I've never seen someone write two functions which call each other and pass accumulator variables around just so that they could have a for loop. I have seen people do that, and worse, to previously clear and readable code, just to get tail-call elimination.
To expand on it a bit: tail-call elimination is a solid example of a leaky optimization. Instead of being something that a "sufficiently-smart compiler" can do for you either behind the scenes or with a configuration flag, it has to leak its way up into the design of your algorithms in order to be useful. Optimizations which leak in this fashion end up obfuscating code in order to squeeze out cycles, and in my book that's not a good trade.
As for the approach of FP people, I don't think so. They might sound a bit arrogant sometimes, but it's no ill intention: it's actually confidence, developed from the broad understanding that functional programming grants you about how you should do things.
I wish more of them would read articles like this one before they attempt to inject their (in many cases) misplaced confidence into other peoples' languages, then. Curiously, I don't see people whose confidence is well-place and rooted in a deeper understanding (e.g., the Simon Peyton-Joneses of the world) demanding Python become more of an FP language,
I've never seen someone write two functions which call each other and pass accumulator variables around just so that they could have a for loop.
You don't require cooperative recursion for iteration.
I have seen people do that, and worse, to previously clear and readable code, just to get tail-call elimination.
Tail-recursive functions are clear and readable. If they aren't to you, make sure you have equal experience with both approaches before complaining. And you're forgetting about the benefits of tail-recursive iteration. It's not that I want to circle-jerk to how functional it is. It has real, tangible advantages including:
No order of evaluation bugs (this is a major plus for complex loops).
Memoization and other optimization techniques are possible.
It's far more easily to prove functional code correct.
To expand on it a bit: tail-call elimination is a solid example of a leaky optimization. Instead of being something that a "sufficiently-smart compiler" can do for you either behind the scenes or with a configuration flag, it has to leak its way up into the design of your algorithms in order to be useful.
You're thinking it the wrong way. It's not that I'm modifying my algorithms in order to use tail recursion. More often than not, I'm modifying my algorithms in order to not use tail recursion because Python chokes with it. It's not that programmers should be forced to write tail-recursive procedures because Python supports them; it's that programmers should be free to if they find them a nicer/simpler/better approach to what they want to implement.
I wish more of them would read articles like this one
If anything, what I've been saying is that I want a multi-paradigm programming language because I understand and value the strengths of all approaches.
Curiously, I don't see people whose confidence is well-place and rooted in a deeper understanding (e.g., the Simon Peyton-Joneses of the world) demanding Python become more of an FP language
Simon Peyton-Jones doesn't care about Python. But some do, and those who do tend to share this opinion. For example, I bet Paul Graham would agree with most of what I'm saying, so might Hal Abelson now that he's working with Python.
Tail-recursive functions are clear and readable. If they aren't to you, make sure you have equal experience with both approaches before complaining.
I do. I think what you're forgetting is that, at least according to everything I've read (I don't have a formal CS background, my degree was a few levels of abstraction higher up, in philosophy), recursion appears to be one of the hardest things to get novice programmers to understand. The fact that it requires special training and even entire books to get people to think and reason recursively indicates that perhaps it isn't the most natural way to approach every problem.
It's not that I want to circle-jerk to how functional it is. It has real, tangible advantages including
Your first two points here are both irrelevant because they apply equally well to other constructs (e.g., comprehensions, a feature in which Python is really second only to Haskell). Your third point is one I'll take a pass on, because I have a long-standing issue with the notion of "provable" programs.
It's not that I'm modifying my algorithms in order to use tail recursion. More often than not, I'm modifying my algorithms in order to not use tail recursion because Python chokes with it
In order to get the most out of, say, Scheme, you have very little choice but to turn anything and everything into tail-recursive functions. Scheme quite deliberately leaves you few to no other outlets for approaching many problems. Couple that with the point above about recursion likely not being the natural way people think about the problems they're trying to solve, and maybe you'll start to see what I mean about this being a leaky optimization.
It's not that programmers should be forced to write tail-recursive procedures because Python supports them; it's that programmers should be free to if they find them a nicer/simpler/better approach to what they want to implement.
Perl programmers have "Perl golf", where they try to pare down a piece of code to the minimum number of lines and, often, the minimum number of bytes and/or language tokens. Perl doesn't require this or force it upon them; they do it because the language and the culture of the language nonetheless strongly encourage it.
Scheme programmers can't golf their programs in quite that fashion, and their language also doesn't completely force them to go to great lengths to achieve some particular programming technique (but see above for caveat), but the language and the culture of the language nonetheless strongly encourage the viewpoint that recursion is the one and only universal answer, the true silver bullet, and that tail recursion is to be preferred far and above all other forms of recursion. I believe this is what Guido means when he occasionally says he doesn't want to turn Python into a "solve everything with recursion" language.
You seem to be under the impression that, by allowing tail call removal into Python, a rush of functional programming zealots would flood into your house and ruin your party forever. You would be forced to go home, dejected; your code would be unclean and unreadable forever more. Perhaps you'd be forced find another language, maybe Ruby. That seems a little silly to me.
But this isn't a worthwhile argument; it's clear your mind is made up. The addition of tail call removal would ruin everyone's code. I'm not sure what you mean by sequence comprehensions allowing memoization, but there you go: iteration is always the most powerful and natural way to express a problem. There could be no reason to use recursion. Make your own stack if it comes to that.
You realize, I hope, that your talk of Scheme is a complete non sequitur. He says "Recursion can be much more natural and readable, sometimes"; you say "In Scheme recursion is the only way!" Well, I mean. Shrug.
1
u/ubernostrum yes, you can have a pony Nov 12 '08 edited Nov 12 '08
I've never seen someone write two functions which call each other and pass accumulator variables around just so that they could have a
forloop. I have seen people do that, and worse, to previously clear and readable code, just to get tail-call elimination.To expand on it a bit: tail-call elimination is a solid example of a leaky optimization. Instead of being something that a "sufficiently-smart compiler" can do for you either behind the scenes or with a configuration flag, it has to leak its way up into the design of your algorithms in order to be useful. Optimizations which leak in this fashion end up obfuscating code in order to squeeze out cycles, and in my book that's not a good trade.
I wish more of them would read articles like this one before they attempt to inject their (in many cases) misplaced confidence into other peoples' languages, then. Curiously, I don't see people whose confidence is well-place and rooted in a deeper understanding (e.g., the Simon Peyton-Joneses of the world) demanding Python become more of an FP language,