Had learned Haskell during a Functional Programming course in school. Had found Haskell a bit difficult to work with. Have now worked a lot on Python. Python is quite easy to work with.
Python does support some functional programming constructs.
Was thinking of revisiting Functional Programming. What would be a better language to code? Haskell or Python? Why?
Haskell is a functional programming language, whereas Python just has some features of functional programming languages. So, this is settled. Q.e.d.
Edit: What is lacking in Python, just to give one example, is the optimization of recursive function calls. This is vital in most real functional programming languages.
Googling, by the way, just produced this nice article.
You'll find many criticisms elsewhere about how Python programmers should not use functional features like lambda. Alex Martelli is particularly good at this. But if you approach functional programming as a Python programmer, you will not get much of a feel for why people are interested in functional programming in the first place. In particular, you will not have access to a powerful static type system and algebraic data types. These tools are definitely part of the Haskell mindset.
Functional programming is not so much a set of features a a way of thinking. To see that way of thinking in action and to start to learn it, check out the paper Why Functional Programming Matters by John Hughes. What you learn from Hughes you can easily apply in Haskell. In fact, Haskell is much more likely to force you to learn that new way of thinking. With Python you can use a few lambdas here and there and fool yourself that you're learning functional programming. You won't be.
That said, Haskell can be a lot to swallow all at once. If you're finding it difficult you might want to try Standard ML or Racket (formerly called PLT Scheme or DrScheme). Standard ML will introduce you to more new ideas, but those people don't have their act together to welcome newcomers in the way that the Racketeers or the Haskell people do.
This question highlights one of my pet peeves about the common understanding of functional programming. Having language features like closures and first-class functions do not make a language a functional programming language. Functional programming is as much about a specific style of programming as it is about specific language constructs. Python has closures, first-class functions, and yes, even a function called "map", but the language itself encourages a procedural, OOP, imperative style of programming; so even if you make use of the functional features, you're still programming imperatively.
As a purely functional language, Haskell basically mandates that one program in a declarative, functional style, so hands-down, Haskell is "more functional" than Python.
While Python does indeed support some functional constructs, Haskell is a functional language. What you also need to consider, is that Haskell is also purely functional and lazy, two concepts that may be the cause of the difficulty you find in Haskell. If this is the case, read on.
What you may want to consider, if, of course, you have the time, is learning another, less complex, functional language. I found that learning a less "aggressively functional" language before moving on to Haskell is a step worth taking. You may want to consider OCaml (Objective Caml), another functional programming language, that is not pure nor lazy. Thus, some imperative programming constructs will be available to you (references, loops, mutable arrays, etc), and you won't have to cope with the monads until you actually want to.
This entirely depends on the time you are willing to spend revisiting functional programming, and on your current background in this same paradigm.
What Boldewyn said. Haskell is one of the most "hardcore" functional languages there is, insofar as there simply isn't any way to maintain mutable state.
That said, Haskell is (unsurprisingly) wildly different from all the imperative and mixed-model languages most people come in contact with, so there would be a learning curve involved in picking it up if one came from a background of, say, VB or C(++) or Java.
Python, on the other hand, features a "Functional Show and Tell" section where a handful of functional features can be test driven within an otherwise predominantly imperative environment.
Thus, if in his original question, the OP's "better" meant "more functional" or "a better citizen of functional-land" (as he has since made clearer), then Haskell "wins" hands down.
From the Haskell homepage:
Haskell is an advanced purely functional programming language. An open source product of more than twenty years of cutting edge research, it allows rapid development of robust, concise, correct software.
Sarcastic translation:
- "advanced" = "Not for everyone"
- "purely functional" = "Definitely not for everyone"
- "more than twenty years" = "And still not everyone likes it!"
- "cutting edge research" = "There is a chance that you may find a practical problem which you can solve with it but we're still working on it!"
- "robust, concise, correct software" = "Are you missing simple, understandable, obvious, readable? Guess why!"
Jokes aside, functional languages aren't meant for the majority and never will be. There are people who can think like a stack but I, for one, prefer that the computer tries harder to understand my gibberish.
精彩评论