r/Python • • Nov 11 '08

Why the hatred for functional programming?

0 Upvotes

86 comments sorted by

5

u/Wiseman1024 Nov 11 '08 edited Nov 11 '08

Okay, I don't think I need to tell why I'm asking this. While Python supports functional programming to a reasonable extent, there's a widespread stubborn hatred of functional programming and anything related to it in the Python community (probably starting with the BDFL), and any proposals for extending Python into something more useful for functional programming (without taking out any of the other ways to use Python, of course, that's the good thing about multi-paradigm languages) is received with ruthless hostility and perceived as an attack against Python.

The Python community is generally a good one (especially compared to others), yet this generalized hatred for functional programming and anything related to it seriously turns me off. While Python is currently my overall favourite programming language (considering the quality of the standard and non-standard libraries, support, and features), I'm currently considering putting my effort and time investments somewhere less hostile to functional programming.

Hostility to functional programming is a form of hostility to abstraction, theoretical correctness and cleanliness. I hope I'm wrong, but this seems like pulling the PHP way. Consistently meeting proposals such as tail call elimination with nothing short of "that's liek difficult to understand d00d ha ha no freaking way" doesn't say much about the health of this community; in fact it looks like something straight out of the average PHP documentation comment.

10

u/DarkGoosey Nov 11 '08

Well I am not really part of the Python community, but I am a redditter and this gives me a bit of exposure I suppose.

From my point of view the biggest issue is the way Function Programming zealots approach the table. There is a pre-existing assumption that everything functional is good and everything imperative is bad. Obviously this is absurd, yet in so many functional-programming articles that flow through reddit there seems to be a subconscious bias toward this line of thinking.

Viewing it from a pragmatic point of view functional programming hasn't really delivered for me. Recently there was a mario-brothers clone written in Haskell (I think) that was hailed as a major achievement. I don't want to be negative towards the authors of that, but it really doesn't seem like a fantastic achievement. In reality it seems to reveal how ill suited purely functional programming is for real world projects.

Of course to swing the other direction and disregard everything functional as academic bluster which is not useful at all in the 'real world' is equally absurd. The fact is that there are a huge amount of things I have taken from functional programming. I program in C++ primarily, although I experiment around with D and it's functional features are the most appealing reason for me to do so.

5

u/Wiseman1024 Nov 11 '08

While functional programming is undoubtedly purer (which is a criterion of good) and academically correct (which is another criterion of good), and it's more useful for specifying and reasoning over procedures and abstractions, I agree that it's weak for real-world projects (in spite of considering it a superior programming technique).

That's why I want a good multi-paradigm programming language. Build the skeleton, the I/O and (as much as I hate this word) the glue on an imperative framework, then implement the application logic in a nice, provable, correct functional manner, and share a powerful object system such as Python's across them. This is my vision of how a real-world application in Python's domains should be built.

3

u/mycall Nov 11 '08 edited Nov 12 '08

Sounds like you just described F# or Scala

1

u/sohang-3112 Pythonista Nov 25 '21

But sometimes, you don't really have that choice of language. For example, if your application uses Data Science, then you pretty much have to use Python - it's the lingua franca of the Data Science world.

1

u/DarkGoosey Nov 12 '08

If you are being serious than I wanted to point out you just reinforced exactly what I was saying with your first run-on sentence.

Why is purer or academically correct criterions of good? How exactly are you defining 'good'? How are you able to call it 'a superior programming technique' while still admitting it is 'weak for real-world projects'?

Since this is heading in the direction of being a religious argument I am going to go ahead and jump the shark here by saying that your viewpoint is coming off as a form of fundamentalism to me. You are starting with the conclusion (functional == better) and working backwards to fill in the blanks. This is not a sane way to approach problem solving, IMHO.

7

u/[deleted] Nov 11 '08

[removed] — view removed comment

9

u/earthboundkid Nov 11 '08

Don't you know? At the Pearly Gates St. Peter's job is to turn away those who can't write a factorial function that doesn't use variables. Being pure is a matter of grave importance for your soul!

-1

u/mycall Nov 11 '08

You mean values, not variables.

1

u/[deleted] Nov 12 '08

Never mind 'purer', how about a definition of 'pure'? In my mind, the purest programming language is the machine code that runs on the bare metal. Everything else is just sugar on top.

-1

u/Wiseman1024 Nov 11 '08

It is. It'll always yield better results and be the easiest to modify (which happens both while you work on a project for the first time, and when you maintain it), and the easiest to understand and reason over it once it has been written, because it's as close to perfection as possible (as abstract and generalized as possible, as simple as possible, as minimal as possible).

1

u/earthboundkid Nov 11 '08 edited Nov 12 '08

(as abstract and generalized as possible, as simple as possible, as minimal as possible).

I think that that works against the goal of being "as readable as possible," which is Python's goal. Even now that I know what the Y combinator does, it still makes no sense to me when I read it. Guido similarly has moved reduce into a library, because he thinks it kills readability. And that is the goal. Not speed (try C or C++), not purity (try Haskell), not flexibility (try Lisp), not chunky bacon (try Ruby). All languages are optimized for different things, but you don't seem to want what Python is offering. Which is fine, but try something else, yeah?

2

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

I think that that works against the goal of being "as readable as possible,"

Aren't you confusing readability with experience reading? After reading SICP and working with Scheme for a while, I find tail-recursive functions as easy to read and understand than loops (actually, easier in some cases, more difficult in others). I definitely find decorators implemented with closures far easier to read than those implemented as functors (a class with __call__). I find lambda easier to read than def Lulz() [...several lines below...] Lulz. I definitely find closures easier to read than the stunts you have to pull to avoid it. I seem to find every functional programming feature I get used with easy to read once I get used with it.

Pretty often, the impression that imperative code is easier to read is just prejudice and trauma from having suffered a Fotran-derived language. I suffered it myself, but I'm getting cured. And I'd like to ask people who learnt to program with Scheme first what they find easier.

All languages are optimized for different things, but you don't seem to want what Python is offering.

I love what Python is offering! That's why I'm using it. I just want it to be even better by pursuing its goals even better.

1

u/mycall Nov 11 '08 edited Nov 12 '08

Have you checked out F#, or should I call it OCaml.NET? It does both OO, imperative, and functional. I like the multi-paradigm languages, although they are harder to learn as there is more than one way to do anything.

0

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

I have, but I'd rather take OCaml for now, at least until we get an F# compiler that doesn't depend on either .NET or Mono.

3

u/arnar Nov 12 '08

You make an excellent point - functional programming isn't the answer to all problems, yet there are tons of useful things to be taken from functional programming.

That's a pretty good reason to incorporate functional ideas in a multi-paradigm language.

1

u/sohang-3112 Pythonista Nov 25 '21

I don't think the OP claimed that Functional Programming is better for every task - he specifically mentioned that the proposal he was talking about (Tail Call Optimization) would not interfere with imperative code in any way. Python is a multi-paradigm language, but it seems to focus too much on imperative code. IMO a few features of functional languages (for example, Pattern Matching), if added to Python, could help a great deal.

1

u/kinghajj Nov 11 '08

Recently there was a mario-brothers clone written in Haskell (I think) that was hailed as a major achievement.

When I saw that, I didn't think, "Wow, somebody wrote something with Haskell!", I thought, "Wow, somebody wrote a Mario-brothers clone, and happened to use Haskell." As the Haskell community grows, more and more useful, "real-world" projects will spring up.

These achievements are not amazing because Haskell is such a hard thing to program in, but because they prove that Haskell can be used to do things that many assume would be hard to do, because they are hard to do in most languages, but aren't hard to do in Haskell.

2

u/[deleted] Nov 11 '08

...more and more useful, "real-world" projects will spring up.

These achievements [...] prove that Haskell can be used to do things that many assume would be hard to do, because they are hard to do in most languages, but aren't hard to do in Haskell.

You're talking as if the future has already come. And no, writing a Mario clone isn't particularly hard in most high-level imperative languages.

1

u/kinghajj Nov 11 '08

I agree it's not, but many would assume it would be harder to do in Haskell, because games are inherently very stateful. My point was that it's not, as that clone demonstrated.

1

u/seunosewa Nov 14 '08

That a Mario clone is remarkable suggests that Haskell is an inconvenient language.

1

u/kinghajj Nov 14 '08

No, the that it is remarkable suggests that popular opinion regarding functional programming is false.

0

u/[deleted] Nov 11 '08 edited Nov 11 '08

its not subconscious at all. The creator of Clojure calls OOP the "spaghetti code of the 90/2000s".

1

u/Wiseman1024 Nov 11 '08

And he's right, in the proper context. The problem is not OOP, it's inheritance. When you abuse an inheritance system (something which is like saying "hello" in Java), you end up with code that's very difficult to follow and maintain. You have to keep jumping from one module to another to get a glimpse of an implementation. It becomes like a puzzle, where you code everything so that it fits with functions inherited from 5 classes ago.

6

u/llimllib Nov 11 '08 edited Nov 11 '08

Besides lack of support for tail calls, which is the only example you give, what makes you think that python doesn't support functional programming? What proposals do you refer to that have been shot down?

My POV: Python spports FP just fine as long as it doesn't conflict with python itself. Remember that Python has:

  • list comprehensions

  • generators

  • coroutines

  • filter map and reduce built-in

  • real first-class functions with closures (albeit those can be a bit quirky)

All of those things point to no fundamental language bias against FP.

2

u/Wiseman1024 Nov 11 '08

Besides tail call elimination, which could be added but and it's not being rejected because it can't be done, but because it's deliberately unwanted, I have:

  • Guido's stubborn quest to make lambda useless by not accepting statements inside it, or by not making statements expressions. He even wanted to remove it at one point, but he saw it was pressing too hard.

  • The closures are pretty real, but the way scopes are handled is inconvenient. It could have been fixed in so many numerous ways so long ago, but it was always an nth-priority. The chosen solution, nonlocal, is acceptable but suboptimal. Expanding lambda into a def with better semantics would have been much, much better, and even more backwards-compatible, since it wouldn't require the introduction of a new keyword.

  • The "flat is better than nested" philosophy is keeping the language from becoming more expressive. In particular, it still has statements. They need to die. I've been talking somewhere else in Reddit about how we could have Python be fully backwards-compatible while turning its current statements (a design wart from the age of Fortran) into expressions. There are countless advantages to that, which is basically lifting a limitation, but I've seen the community even wants an arbitrary limitation to stay just so that you cannot write more flexible code.

  • filter, map and reduce? You're pointing out a weak spot, not a strong one. First of all, Guido actively discourages their use in most cases, and he's moving reduce out of builtins (he also expressed his dislike of it many times).

  • More importantly, Python ships with an extensive standard library to handle text in many ways, lots of class utilities, lots of pretty much everything, and almost no FP-related utilities in comparison. The FP utilities have been neglected since ever. Where are promises, futures, foldr, foreach/repeat/other functional loop constructs, scanl/r, flip, prog1, progn, conses, memoizators...?

2

u/[deleted] Nov 12 '08

What you're saying is reasonable, but it sounds like Python isn't your cup of tea, so why don't you use a different language?

I'm not trying to pick a fight, it just sounds like Python pisses you off.

1

u/Wiseman1024 Nov 12 '08

The reason why this anti-FP attitude (or, at least, FP neglect) pisses me off and I'm passionate about it is that I care for Python, because I otherwise love it. I'd like to settle with Python as my main language for several years, and I'm just complaining about what's keeping me from doing this.

It's just because Python is so close to my ideal language that I bother to bitch about what I don't like from it; I don't bother with, say, Perl or PHP because they are pretty far from what I want.

3

u/llimllib Nov 12 '08 edited Nov 12 '08
  • Guido prefers named functions to unnamed ones; it's his language, how does that affect the functionality of the language?

  • The scope handling is what I was referring to as a bit quirky. I can see this as a beef with the language, and might even agree somewhat, but I don't see it in the light of opposition to FP that you do.

  • Again, I cautiously agree while seeing your point as largely orthogonal to your original hostile-to-FP complaint.

  • Who cares if they're in builtins or functools? Or if Guido prefers spelling filter(lambda x,y: something(x,y), collection) as [something(x,y) for x,y in collection]?

  • It definitely doesn't ship with an FP toolkit, and the stdlib doesn't return self on some things that are truly maddening. However, I see that as being because Python is not a primarily functional language, and so the builtins need can reflect that.

However, the language also doesn't do anything to prevent you from adding those. If anything, I think the language should be characterized as cautiously tolerant of FP style, with one big exception, tail calls.

(Furthermore, you're making fine rational arguments (in our thread at the least) and people shouldn't be downvoting you. Raise your level, redditors.)

2

u/Wiseman1024 Nov 12 '08

Guido prefers named functions to unnamed ones; it's his language, how does that affect the functionality of the language?

It doesn't affect the functionality outside namespace pollution, but it affects readability. Wasn't readability a goal of Python?

Who cares if they're in builtins or functools?

Not too important (every of my modules does import * from functools on top), but it shows the focus. You asked if there's a fundamental bias against FP and I was replying to that.

However, I see that as being because Python is not a primarily functional language, and so the builtins need can reflect that.

Python is not primarily a language for writing HTTP servers, yet it ships with one in the standard library. Even if it's not the main focus, FP could be given some attention.

(Furthermore, you're making fine rational arguments (in our thread at the least) and people shouldn't be downvoting you. Raise your level, redditors.)

My grandparent post was seen as criticism on Python, so I guess the fanboys will downvote. Since there are more fanboys than people willing to engage in rational discussion, the total score is going to be negative. Same as for the whole Reddit post.

1

u/khammack Nov 12 '08

Guido's stubborn quest to make lambda useless by not accepting statements inside it,

Isn't the whole concept of 'statements' counter to functional programming?

The "flat is better than nested" philosophy is keeping the language from becoming more expressive. In particular, it still has statements.

So you want lambda to accept statements (which would make it less functional), then you want statements eliminated from python.

The closures are pretty real, but the way scopes are handled is inconvenient.

I can hardly view this as "hatred" for functional programming.

More importantly, Python ships with an extensive standard library to handle text in many ways, lots of class utilities, lots of pretty much everything, and almost no FP-related utilities in comparison. The FP utilities have been neglected since ever. Where are promises, futures, foldr, foreach/repeat/other functional loop constructs, scanl/r, flip, prog1, progn, conses, memoizators...

What stops an enterprising team of functional programmers from creating an open-source functional library ala "numpy"?

1

u/Wiseman1024 Nov 12 '08

So you want lambda to accept statements (which would make it less functional), then you want statements eliminated from python.

I want either. Of course, it'd be best if we had no stinking statements, but either would serve to make lambda work better.

What stops an enterprising team of functional programmers from creating an open-source functional library ala "numpy"?

Nothing, I have mine too (and will publish it once I deem it properly tested). I was replying to llimllib's post on bias against FP.

6

u/sard Nov 11 '08 edited Nov 11 '08

that's the good thing about multi-paradigm languages

I don't think the BDFL is interested in creating a multi-paradigm language. He's much more interested in a 'one obvious consistent Pythonic way of doing everything' language, and that one way historically hasn't been functional.

3

u/Wiseman1024 Nov 11 '08

Then instead of getting the best possible language in its domain, he'll get the most Pythonic language, for an arbitrary and sometimes bizarre definition of Pythonic.

5

u/earthboundkid Nov 11 '08

There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.

3

u/mycall Nov 11 '08

Don't languages that have more than one way to do something appeal to more people and thus service more requirements and applications in the long run?

3

u/earthboundkid Nov 11 '08

No language can be perfect for everyone. I tried to make Ruby perfect for me, but maybe it's not perfect for you. The perfect language for Guido van Rossum is probably Python.

— Matz

-1

u/Wiseman1024 Nov 12 '08

Wise. But then:

[...] Larry Wall, who is my hero actually [...]

ಠ_ಠ

2

u/[deleted] Nov 12 '08

Sometimes, languages that have more than one way to do it suck. Perl, for example.

My opinion, but really most of this discussion here has been spiraling downward into the dead-horse-beaten hellhole of language flamewars. Plus, I do appreciate Perl for what it did well and the positive impact it's had on other languages, like Python.

1

u/Wiseman1024 Nov 11 '08

From my other post:

There should be one-- and preferably only one --obvious way to do it.

I used to think this is a good idea, but I'm seeing it leads to "Pythonic", and "Pythonic" leads to a sub-optimal language.

Although that way may not be obvious at first unless you're Dutch.

I'm not worried if it's not obvious. I'm worried that it's not good.

2

u/earthboundkid Nov 11 '08 edited Nov 12 '08

This goes back to my point: if you don't like the pythonic style, don't use Python. I guess what you want is for someone to take the Python library and to adapt it to work with a Lisp-like language or something. But if that's the goal, why not just use Clojure which has the whole Java library?

1

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

I'm seriously considering writing a Lisp compiler for CPython myself. (A Lisp-1 with proper tail recursion, of course.) Something like Lython or Psyche, updated for today's Python. I think those were awesome ideas, and I'm saddened to find they're abandoned.

If only I had more time. I have to choose between doing what I want to get done, or doing what I'd like to have to do what I want to get done.

I'm also considering moving to Clojure once it's a bit more mature, or perhaps making a wilder jump to Erlang. I'm terribly attracted to obscure Schemes with object systems, but I know that's going to have no support, no community, and pretty often no documentation (heck, even Tiny-CLOS for PLT is largely undocumented).

1

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

For the love of all things good, can you explain me why would you downmod a post where I'm talking about implementing something myself? Would you rather me not release it if I finally do it? To that extent do some redditors' flamboyant fanboyism get?

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.

2

u/aieee Nov 11 '08 edited Nov 11 '08

"Hostility to functional programming is a form of hostility to abstraction, theoretical correctness and cleanliness"

I don't think the community or the BDFL are hostile to these things, more generally conservate and hostile to certain coding styles.

The Python way is to prefer easy digestability over things like proper lambdas, and reduce. Lambda is crippled to stop people writing ugly code. Reading the occasional blog post with ugly Ruby code makes it easier to understand why this choice was made.

I would personally prefer more support for functional programming, but I sort of understand the logic of not accepting functional features without carefully judging the trade-offs with the 'pythonic' way.

-1

u/Wiseman1024 Nov 11 '08

I don't think the community or the BDFL are hostile to these things, more generally conservate and hostile to certain coding styles.

Which is exactly what I've said.

Lambda is crippled to stop people writing ugly code.

Okay. Then let's cripple classes, inheritance, operator overloading, if-elif sequences, list comprehensions, regular expressions, string manipulation, template languages, and pretty much everything too.

5

u/aieee Nov 12 '08 edited Nov 12 '08

Which is exactly what I've said.

To be clear, I'm saying they're not hostile to "abstraction, theoretical correctness and cleanliness" per se, but the possibility of features enabling those things leading to unpythonic code.

Okay. Then let's cripple...

I'd prefer proper lambdas, but if I kinda squint, I can see Guido's logic in forbidding them, since named functions accomplish the same thing.

To make the distinction clear, though perhaps it is a small distinction: Python is not especially hostile to functional ideas - but it is hostile to the kind of hard-to-decifer code that the functional style can produce, especially if there is a fairly straightforward alternative.

Again, I'd like Python to be more functional for certain situations, and is the reason I'm sniffing around other languages. But I think 'hatred' is far too strong a word to describe the Python attitude to functional programming.

In this light, Guido's rational for removing reduce seems reasonable (though arguable), "almost every time I see a reduce() call with a non-trivial function argument, I need to grab pen and paper to diagram what's actually being fed into that function before I understand what the reduce() is supposed to do" http://www.artima.com/weblogs/viewpost.jsp?thread=98196

Edit: on further reflection, and reading your other replies, I pretty much agree with you. The fear of introducing functional concepts in the python community is exaggerated. However, this natural conservatism may be part of what has made Python the otherwise great language and community it generally is.

1

u/Wiseman1024 Nov 12 '08

I'd prefer proper lambdas, but if I kinda squint, I can see Guido's logic in forbidding them, since named functions accomplish the same thing.

Then let's forbid strings, since array.array('c') accomplish the same thing.

Python is not especially hostile to functional ideas - but it is hostile to the kind of hard-to-decifer code that the functional style can produce

I don't find tail-recursive functions, nested lambdas, reductions, etc. hard to decipher, definitely not harder to decipher than imperative loops with several variables. I think it's just prejudice against the unknown, and since people come from the damned Fortran line... Maybe there's a similar amount of prejudice against while loops from people who come from Scheme.

"almost every time I see a reduce() call with a non-trivial function argument, I need to grab pen and paper to diagram what's actually being fed into that function before I understand what the reduce() is supposed to do"

I think/hope it's an hyperbole of his; in other words I can't believe it. I regard Guido as smarter and more experienced than I am (albeit a FP hater ;) ), and I don't need paper to understand reductions.

1

u/[deleted] Nov 12 '08

Frankly, if you're writing a lot of one-time use functions in python, you're not writing python correctly.

1

u/[deleted] Nov 12 '08

Seriously grumpy, you need to find a new language. Who has the gun to your head?

I realize you are a troll, I should not even be responding to you. . .

3

u/cc81 Nov 11 '08
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

2

u/Wiseman1024 Nov 11 '08

I know, the Zen of Python.

Beautiful is better than ugly.

Yay, an argument for functional programming.

Explicit is better than implicit.

Yay, an argument against Perl.

Simple is better than complex. Complex is better than complicated.

Yay, unless Guido thinks Fortran is simple and Scheme is complex. If so, I suggest he tries assembly, he'll find it the simplest language ever.

Flat is better than nested.

Bingo. This is a fundamental flaw of Python's philosophy. This is so wrong I don't know where to start. This is an argument against abstraction, against flexibility, against expressivity, and against anything that's good in a programming language. I'm starting to think I should take my previous suggestion seriously, and write Python about the wonderful language assembly which has no nestable elements.

Sparse is better than dense.

More arguments against Perl.

Readability counts.

An argument for functional programming and nesting without requiring you to jump when reading code.

Special cases aren't special enough to break the rules.

Okay, at least he got this right: he doesn't want Perl.

Although practicality beats purity.

Meh. Only to a certain minimal extent. And it's disgusting and shameful when it does. It shouldn't be written here, it may spoil people.

Errors should never pass silently.

Yes, please.

Unless explicitly silenced.

You mean, like PHP's @ ? (j/k)

In the face of ambiguity, refuse the temptation to guess.

Of course. I think I fully agree with Python's syntax philosophy (except for the little bit where less powerful cousins of expressions exist for no reason, serving only as a left-handed hammer since you already had expressions and they were much better).

There should be one-- and preferably only one --obvious way to do it.

I used to think this is a good idea, but I'm seeing it leads to "Pythonic", and "Pythonic" leads to a sub-optimal language.

Although that way may not be obvious at first unless you're Dutch.

I'm not worried if it's not obvious. I'm worried that it's not good.

Now is better than never. Although never is often better than right now.

Agreed.

If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea.

Yes! And if the explanation is the implementation, you've achieved Satori. In other words, you're programming functionally. Now let's pay proper attention to FP, please.

Namespaces are one honking great idea -- let's do more of those!

Yes, but don't lose the focus: it's lexical closures the number one honking great idea.

6

u/kinghajj Nov 11 '08

Beautiful is better than ugly. Simple is better than complex. Flat is better than nested. Readability counts. If the implementation is easy to explain, it may be a good idea.

And functional programming helps with all of these goals, especially if (gasp) you optimize tail calls! Recursion is beautiful.

2

u/nevare Nov 11 '08

Python is also my favorite language and what I miss the most from functionnal programming are

  • tail call optimisation (I think I heared people didn't wanted it ... but maybe it was just never implemented ?)

  • more powerfull lambdas

  • implicit return of the last executed line (it makes you feel you are in a functionnal language)

The last two points are solved in ruby, but they didn't implement tail call optimisation either.

1

u/Leonidas_from_XIV Nov 12 '08

I don't like the way how Ruby does it, since the last line is pretty random if the programmer didn't mean to return it explicitly.

1

u/nevare Nov 12 '08

True enough. But I wouldn't mind it if I could write

def f(x): if cond(x): modified(x) else: other(x)

which is pretty how you would write it in Haskell. If you have no side effect there is no need for several lines, so the return is implicit.

1

u/Leonidas_from_XIV Nov 13 '08

I know, this is how it's done in Scheme but it has clearly a more functional approach, whereas Python does rather go the iterative way. Not to mention that explicit is better than implicit in Python.

And yes, I also like the chaining that is possible with jQuery because it consistently returns the changed results.

1

u/nevare Nov 13 '08 edited Nov 13 '08

Yeah this chaining is cool but it is possible with any object programming language. I guess a method returning the objet on which it is when there is no explicit return could help make this a more common thing, but I'm not so sure about this idea.

Anyway you can make jquery in python. I even started doing it.

1

u/ianb Nov 12 '08 edited Nov 12 '08

Python isn't hostile to functional programming. It's just not. Yes, people regularly show up on lang.python or python-dev and complain about functional things. It's called trolling, and reading trolls is not how you learn about a language.

It's the nature of the arguments made that make the Python community seem hostile. For instance, someone will get all hot and bothered about the filter function, based on (incorrect!) rumors that it would be eliminated. They show up, complain about it, make some sweeping generalizations about Python and functional programming. And of course people take the bait, and an argument ensues, and some overzealous people say "Python isn't a functional programming language". Of course this is the internet, and anyone can make sweeping claims about Python on either side of this debate. If you look at the names of these people, you'll find they are just regular joes who follow along on the lists, and sometimes get a little ahead of themselves -- these kinds of claims don't come from core Python developers or from Guido.

There is some hostility to the introduction of Lisp features when they don't fit well. filter is an example of this -- instead the preferred form in the community is a list comprehension, which was a language form taken from Haskell. This is hardly an anti-functional-programming stance. Is it wrong for Python to be a little picky about which functional programming languages to take ideas from?

Other complaints aren't even that clear. Yes, writable outer scopes have only just been added, I wish they were added previously, but writing isn't "functional", so really once again you are asking for Lisp patterns, you aren't asking for functional programming support.

Tail call elimination is another hot topic, but I think it makes no sense for Python -- and maybe if tail call elimination didn't cause problems it might have gone in. But that's not the case.

Also, I've yet to see anyone appreciate what a nice functional feature decorators are. All I see is people complaining about things from Lisp they can't find in Python, and can't be bothered to implement themselves (it's not like they'd be hard!) If you think a little more deeply about what you really want, I think you'll find it in Python. Well, so long as you don't need strict functional programming, which does offer advantages (as in Erlang, where it's at the core of its concurrency) -- Python is a multi-paradigm language and nothing is strict anything, but I think everyone is aware of that.

0

u/[deleted] Nov 12 '08

For all who are reading this thread: Wiseman1024 is a typical functional programming troll, a subspecies of the common internet troll.

1. His user name is "wise man" which means he thinks he is smarter than you.

2. He make extreme claims in order to get attention

Hostility to functional programming is a form of hostility to abstraction, theoretical correctness and cleanliness

3. He personalizes purely technical arguments

there's a widespread stubborn hatred of functional programming and anything related to it in the Python community (probably starting with the BDFL)

The cardinal rule of internet discourse is "don't feed the trolls," but I'm breaking this now in order to send as strong a message as possible


Wiseman1024, I STRONGLY SUGGEST you follow this course of action you lay out:

I'm currently considering putting my effort and time investments somewhere less hostile to functional programming.

The Python community is a pretty tolerant bunch, but they are intolerant of personal attacks, over-generalizations, and, to a large extent, optimizations that risk making the CPython interpreter substantially more complex than it already is. In addition, you seem to not care about some of the core values of Python community, namely practical applications and real-world problems. You even admit this:

I agree that [functional programming] is weak for real-world projects (in spite of considering it a superior programming technique).

The problem is that your idea of superior is incompatible of the core values of the Python community. Since it seems unlikely that either will change, I suggest you proceed accordingly.

-2

u/crucialfelix Nov 11 '08

I see lots of functional programming in python. in fact more than in many other OOP equipped languages. I think its due to the clear namespaces. in php people tend to use classes or objects as a kind of namespace. in python I usually see modules filled with useful functions.

haven't noticed any hostility.

4

u/Wiseman1024 Nov 11 '08

Functional programming is not about using functions instead of classes; it's about a general collection of features and abstractions. You can read about the most common ones here: http://en.wikipedia.org/wiki/Functional_programming

-7

u/neoform3 Nov 11 '08 edited Nov 11 '08

You would like to know why people hate functional programming..

I would like to know what the hell is wrong with python programmers that they feel the need to constantly attack PHP. It truly makes no sense whatsoever. No other group of programmers (other than python coders) attack any other language, they way they do to PHP.

The only conclusion I can come to is it's because of jealously. Python coders wish more people used python instead of PHP..

The best part is, despite my having asked/commented in logical way.. I already know this reply will be modded -20 within an hour, which does nothing but proving my point.

11

u/earthboundkid Nov 11 '08

I think it's because we've all used PHP and when we think back on it, we think, "Sheez, why did I waste time on that?" There's a bit of shame at how bad our PHP code was mixed in with the conviction that you can't possibly write good code in PHP (since I didn't, QED).

Plus, just as a matter of fact, PHP is a very poorly "designed" (to use the term loosely) language. Which gives us an element of fact to hang our hate on.

-3

u/neoform3 Nov 11 '08 edited Nov 11 '08

You know, I've coded in VB.NET before, I think it sucks and wouldn't use it, but.. I also never talk about it. I don't like it, I move on and learned something I did like instead.

Seems like the same thing happens in the Postgre community as well (now that I think about it most python users are postgre coders), and they never stop railing against mysql.. I never hear mysql users attacking postgre, or complaning about how it's far more complicated and more difficult to set up or learn..

Why the dwelling and hate?

Also, saying you can't write good code in PHP is just false.

5

u/earthboundkid Nov 11 '08 edited Nov 11 '08

Also, saying you can't write good code in PHP is just false.

I think you need to attend to my argument more clearly:

  1. I tried to write good code in PHP.

  2. I failed.

  3. Therefore, no one can write good code in PHP.

QED ∴

It's logically indisputable.

1

u/neoform3 Nov 11 '08

Can't fight that logic.

1

u/Wiseman1024 Nov 11 '08

I would like to know what the hell is wrong with python programmers that they feel the need to constantly attack PHP.

I'm a long-time PHP user, and I know of its strengths and relative strengths. I still think one can write acceptable code with it, provided you build a good framework to interface the standard library. What I'm attacking, though, is that the language seems to be designed by inexperienced programmers who are in no position to design a programming language and keep coming up with crap and have little idea of how to implement a proper language and standard library, and what's even worse, a horde of PHP "programmers" who write the worst pieces of shit my poor eyes have ever seen, making use of every single bad practice I can think of with whatever PHP gives them. To make matters worse, this horde of Pretty Horrible Programmers is actively encouraged to keep working this way.

No other group of programmers (other than python coders) attack any other language, they way they do to PHP.

I attack Java more than I attack PHP. I enjoy writing PHP more than I enjoy writing Java. It's just that I know how PHP is the butt end of jokes here, and I wanted to show how some Python programmers are gravitating towards what they supposedly loathe.

The only conclusion I can come to is it's because of jealously. Python coders wish more people used python instead of PHP..

Yes, but please, not the idiots who use includes like functions, work in the global scope, constantly enter and exit PHP in the middle of anything, don't indent code, do all sorts of crap with extract and variable variables, completely misunderstand the type system (especially the concepts of truth and existence), write PHP like they write C, and so on.

3

u/neoform3 Nov 12 '08

don't indent code

I doubt you'd get far in python if you don't indent.. ;)

3

u/[deleted] Nov 11 '08

I think it's largely in part that no one is going to defend PHP. If you bash perl in a python group, you're going to get replies claiming perl has it's place. You have to be stupid to defend PHP.

3

u/neoform3 Nov 11 '08

I like PHP. I like Python.

Both have their place.

PHP is good for rapid development. Python is good for well organized development.

I can bang out a PHP site quite a bit faster than I can a python site with identical capability.

3

u/Singletoned Nov 11 '08

You need a single page with a form on it that a user fills out and it returns a thank you message. It might need to scale to a million users in a few days.

What would you build it in? You would need a very, very good reason not to say php. (And I say that as someone who hates php).

1

u/[deleted] Nov 12 '08 edited Nov 12 '08
import cgi
form = cgi.FieldStorage()

name = form.getvalue('name')
msg  = form.getvalue('msg')

if None in (name, msg):
   displayform()
else:
   processcomment(name,msg)

1

u/[deleted] Nov 12 '08

What would you build it in?

It doesn't matter, because your language isn't going to be the bottleneck here.

1

u/[deleted] Nov 12 '08

Perhaps it's because a lot of python programmers started using python for websites, and found it to be vastly better than php?