r/Python • • Nov 11 '08

Why the hatred for functional programming?

0 Upvotes

86 comments sorted by

View all comments

Show parent comments

4

u/ubernostrum yes, you can have a pony Nov 11 '08

Well, mostly there seems to be resistance to turning Python into Scheme, because, well, if you like Scheme it's already there for you to use (I'd make a joke here about how a future revision of Scheme should turn non-tail-recursive functions into a compiler error, but I'm afraid somebody would actually think that's a good idea). Oh, and for the record: I'm against tail elimination in Python simply because it seems inevitably to lead to people writing obfuscated, hard-to-read code simply for the sake of having a tail-recursive function, and Python is, well, not about that: it's about clear, readable code.

But as others have pointed out, one of the biggest problems, to me as a Python programmer, is the zeal with which FP folks approach this; it's not a case of "we want the ability to do more FP stuff in Python", it's "FP should be the one and only way in which programmers are allowed to write code". You yourself are leaning that way with your straw-man "hostility to abstraction" swipe (hint: OOP is also abstraction. It's different abstraction than FP, but it's abstraction nonetheless).

Meanwhile I'm quite happy to take advantage of closures, comprehensions, coroutines, function composition and all the other things Python lets me do, without thinking that has to be the only way I think about or write code. So maybe the problem is in yourself and not in Python :)

3

u/arnar Nov 12 '08

I'm against tail elimination in Python simply because it seems inevitably to lead to people writing obfuscated, hard-to-read code simply for the sake of having a tail-recursive function, and Python is, well, not about that: it's about clear, readable code.

That's a pretty weak reason. You can (ab)use many of Python's features to make extremely obfuscated code - such as decorators, metaclasses, context managers etc. etc.

The best reason (although I don't think it is a good one) against tail-call-elimination is confusing stack traces in case of exceptions.

Tail call elimination also actually allows you to write clearer code while still being efficient.

1

u/Wiseman1024 Nov 12 '08 edited Nov 12 '08

Exactly. And stack traces can be fixed as well by just keeping the first and the last call in the stack (or just the last), along with the number of omitted calls. It'd require an additional property in stack frames: the number of tail call to the same function it is. This would still mess with cooperative recursion, but that's a lesser issue.

1

u/arnar Nov 12 '08

Besides, it wouldn't be to hard to supply a debug parameter that turns off tail call elimination. If you are debugging an exception where the stack indicates eliminated frames - you would just turn it on and see the full exception.

Of course, then your stack might blow long before you get your exception though :)

1

u/Wiseman1024 Nov 11 '08

Well, mostly there seems to be resistance to turning Python into Scheme, because, well, if you like Scheme it's already there for you to use

Scheme doesn't have Python's object system, syntax, standard or third-party libraries.

(I'd make a joke here about how a future revision of Scheme should turn non-tail-recursive functions into a compiler error, but I'm afraid somebody would actually think that's a good idea).

Fuck! I'm writing an SFRI for this tomorrow! :)

I'm against tail elimination in Python simply because it seems inevitably to lead to people writing obfuscated, hard-to-read code simply for the sake of having a tail-recursive function, and Python is, well, not about that: it's about clear, readable code.

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.

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. Regardless, every proposal or comment I've read from the FP folks (including my posts, since I'm probably included in that group) is about extending Python so that it also allows a functional approach, not about "let's take Guido's favourite feature out huh huh". How would any of what I suggest hurt anything that has been done in Python so far?

And OOP is an abstraction, of course it is, and I like it, and I miss it when I'm using Scheme. Abstractions are a honking great idea -- let's do more of those!

Meanwhile I'm quite happy to take advantage of closures, comprehensions, coroutines, function composition and all the other things Python lets me do, without thinking that has to be the only way I think about or write code. So maybe the problem is in yourself and not in Python :)

If I weren't doing this, would you think I'd even bother to use Python? The only reason why I'm sticking to it is because it's the closest language to my ideal multi-paradigm programming language with a good first and third-party library and support. If Python and Scheme were breeds of dogs, I'd have them mate and create the best, cutest, smartest dog ever. Think of crossing a Python with this: http://lispers.org/ . Bah, simply adding a bit of the lovely secret alien technology to the current Python would make it absolutely lovable.

1

u/ubernostrum yes, you can have a pony Nov 12 '08 edited Nov 12 '08

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,

1

u/Wiseman1024 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 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.

0

u/ubernostrum yes, you can have a pony Nov 12 '08 edited Nov 12 '08

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.

1

u/cunningjames Nov 13 '08 edited Nov 13 '08

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.