Knowledge of what functional language will upgrade my coding skills in Python?
-
I want to improve my Python coding skills by learning the way of functional programming. My initial plan of action was to learn Haskell, since it is a purely functional language. But, then I came across Scala, which combines the paradigm of object-oriented programming and functional programming and to be honest sounds lucrative. Scala is closely associated with Java and I feel like I am distancing away from Python If I choose to learn Scala. What would be a better option? My goal is to imbibe the best practices of functional programming and put them to use in Python.
-
Answer:
Learn Haskell. There are two ways your experience with Scala is likely to go: You'll use Scala like Java or any other imperative programming language. If you do this, you won't learn anything new other than Scala syntax, and you won't grow much as a programmer. You'll do your best to write functional code in Scala, and you'll find yourself bending over backwards to do things that should be natural. You'll think: "it should be easier to abstract this pattern" "this code should be cleaner" "this language is so complicated!" And then you'll discover that Haskell solves most of your problems in a much cleaner and more satisfying way. You'll try out Haskell's Hindley-Milner type inference and you'll think "this kicks local type inference's ass!" And you won't want to go back to Scala. For me, and I think many others, Scala was a gateway drug to Haskell. It helped me understand basic Haskell concepts like Monads (via Scala's for-comprehensions), but it basically left me wishing I'd tried Haskell first. Any programming problem you can dream up can be solved in Haskell, although often in a very different way from how you might conventionally do it in an imperative language. I find that having the Haskell-y solution in the back of my mind while I write imperative code is often helpful for suggesting good ways to structure algorithms and decouple components. The only downside when writing Python is that experience with Haskell makes me wish Python had better support for functional programming.
David Simmons-Duffin at Quora Visit the source
Other answers
Unfortunately you're going to find that Python basically does everything a bit poorer than some other language. Its only real positive over anything else is its syntax structure, and even that's subjective (as some programmers actually find it irritating). So no matter which proper FP language you use (and I'd also suggest Haskell as the 1st to look at, though Scheme's a close second) it's going to make Python look like a rabid dog. Even if you go for a more proper OO language (e.g. Scala) you're going to feel Python's inferior OO concepts as a failure. And inferred typing is going to make you ask why Py is using Dynamic. So be warned, you might loose some respect for Python. But no fear, old hands will tell you there's no single "awesome" language. They all have their warts, some just have a bit more. The trick is to learn from this, it'll make you much better even in the "bad" language: "Knowing where the trap is - that's the first step in evading it." (from Frank Herbet's Dune)
Irné Barnard
People usually go the other way. Python is a good language to get half way from Java to an FP language. It's not a great FP lanaguage, but it is distinctly better than (traditional) C++ / Java (though I know they're evolving in an FP direction these days.) So you can practice some FP ideas in Python, but then eventually you'll think of moving to a dedicated FP language. However, there are good ideas which are standard / required in FP that you can optionally use today in Python : - higher order functions. You probably know about these already. But be willing to use them. You will start to miss proper anonymous functions / lambdas though. - closures can often be used instead of objects, and can sometimes be simpler. - Python has perfectly good Comprehensions. - partial application is a really good idea. And one which I didn't really grok fully until I started playing with currying in Haskell. I use it a lot in Clojure, and I'll probably start using it more in Python. - laziness is great. Lazy lists in Haskell and Clojure can be emulated (somewhat more verbosely) with Python Generators. - pattern-matching / destructuring in function arguments is really nice and makes code a lot more concise and clear. But sadly there's not much you can do to emulate that in Python - Python has no tail-call optimisation, so you'll have to use iteration instead of recursion and that's also more verbose - Python's decorators are far inferior to real macros, but they're better than nothing and can be put to good use.
Phil Jones
Adding to , I too would suggest learning Haskell. I loved reading http://learnyouahaskell.com/ (LYAH) for this. Even if you don't actually write Haskell, just learning about it will affect the way you think about programming and improve your abilities in general. I was able to do some extensive amounts of functional programming in Python, utilizing some features which support doing that. It was all inspired from the concepts gained from LYAH and my own Haskell experience. Scala, though is great in the kind of functional programming it offers, but it also brings in too many other features which somewhat make the functional part hard to spot. This is subjective though, as I have very less experience with Scala.
Rohan Jain
For those of us who are not into "purity" of haskell, ocaml might provide a good alternative. There are some interesting projects like http://openmirage.org/ and http://www.frenetic-lang.org/overview.php . Frenetic is actually implemented in both; Python and OCaml
Azzie Elbab
Related Q & A:
- What My Baby Will Look Like?Best solution by Yahoo! Answers
- What acne medication will help me the best?Best solution by Yahoo! Answers
- What unknown villian will Colm Feore play in the upcoming Thor Movie?Best solution by Yahoo! Answers
- What programming language would be appropriate for better job opportunities?Best solution by quora.com
- What is better medical billing and coding or medical assistant?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.