Ask HN: Why and how should someone learn functional programming?
Thanks in advance.
Thanks in advance.
* Natural terseness. A lot of functional code is freakishly short. Because functional code is similar to how we think of math, it's often times easier to simplify out over-computation and you end up with the bare minimum of what you actually need.
* Generic programming. Things like currying and first-order functions are great for separating (or combining) context from computation. When you understand where the difference lies, you can write your code such that it is adaptable in the most efficient way (read: generic).
* Lambda functions. Infinitely useful in a language that uses them, like Python. Lambda's are great for doing work over large collections of data without getting lost in for loops (i.e. map, reduce, sort, fold).
FWIW: I learned what I know from Haskell
I got used to short functions so much, than my normal function length in any other language went down a lot too. Now I'd probably refactor anything that doesn't fit on half of a screen (got fairly big monitors though). And that's a good thing - the effect is easier to read and the shorter the code is, the more self-documenting it becomes, because you have to name every sub-process correctly via the function name. Something that would otherwise be a 40 line function with inline comments: "First do this", "then do that", becomes a two-line: do_this(); do_that(); with ~20 lines in each of them. Much much better.
On the other hand, once you get too used to lambdas, you start missing them in other languages. It just felt natural for me at some point to make a C macro (f, arr, type, len) which did something like map on an array of `type` using a function pointer `f`. That didn't go very well with other people who normally read that code...
It's a little bit silly, but it's a great bottom up tutorial of haskell and a lot of functional programming paradigms.
The biggest reason that its' a good thing to learn a functional language is a lot like the reason that it's a good idea to learn more languages than just Java. In this case, all you have is one paradigm to think in (object-oriented (probably procedural as well, but...)). Functional programming is a completely different beast that requires different ways of thinking. Lots of the neater concepts of many popular languages now are lifted right out of functional ones. Python's list comprehensions and generators come to mind.
A more pragmatic reason to learn about functional programming is that it is becoming increasingly useful. Functional languages are, in general, much easier to parallelize. More importantly than that though, they make it easier to make _correct_ parallel and multi-threaded programs.
As for why these are often presented as the 'holy grail' of programming, many of features exist to make it simple to create concise, correct programs. The features are all the foreign-sounding buzzwords that people tend to rave about: composition, laziness, currying, lambda-functions, monads, strong type systems, type inference, higher-order functions, loop abstractions, and any number of other nifty things.
If you're interested in learning more, check out the Haskell or Ocaml languages (or Lisp, but Lisp isn't entirely functional).
Realworld haskell is free online, any others?
http://learnyousomeerlang.com/
http://www.freetechbooks.com/functional-programming-f34.html
http://www.freetechbooks.com/introduction-to-objective-caml-...
I remember seeing lots of resources for erlang, scala, F#, haskell, clojure, ocaml, scheme and CL in delicious. Probably each of these lang's has a stackoverflow thread "What's so good about.."
you learn the same way you learn any other language, just pick something to program and go for it.
Use the 3 schemer books The little schemer, the seasoned schemer and the The Reasoned Schemer, and read Structure and Interpretation of Computer Programs. You can work through the books using clojure or other more appealing languages.
Its not the "holly grail of programming", its more like "the less broken type of programming".
A list of neat projects that should highlight the use of functional programming would be useful in this case.
I've seen a bit of "funcational" in python's type "function" and lambda expressions. But didn't really understand how I could go far with this.
I kinda don't see how I can build a large project based solely on the idea of having those constructs in the language.
Erlang taught me a lot that I never even came close to learning with PHP, Python, and C++. Most notably what Erlang is good at - Concurrent Programming.
I still didn't fully "grok" functional programming though as I just completed the book and didn't complete any projects using Erlang. The real meal first started with Common Lisp which drove me to Scheme (Common Lisp is a mess, Scheme is minimal in nature, and Scheme is much more true to a functional language than CL). I then picked up the Little Schemer series of books and proceded to have my world view enlarged; with the addition of many other excellent resources, my knowledge of programming increased exponentially.
Within a few months of "grokking" functional programming and grooving with Scheme (it is my favorite language to date, Python comes second) the noted benefits were: an increased academic understanding of computing and the modality with which computable logic is represented, clearer/cogent/lucid and higher-caliber thought, my creativity in programming was unleashed, and I've found that I have a vested interest in the philosophy behind what I do. I also feel like my general perception of reality has changed too, I interpret my life experiences with a different set of concepts now.
The experience of learning a functional language and being committed to "grokking" it, has been remarkably similar to the time I went backpacking through India for two months on a budget...alone. I came back with a new kind of maturity and my mind just works different from everyone else I know that still hasn't left the states. It's like that, your mind just works different than all the others that haven't left procedural/OOP land.
As for production projects: there are plenty of companies that use Scheme, Erlang, and a other functional languages. I personally would love the opportunity to program in Erlang or Scheme over any other language (they are that much of a joy); alas, my industry (web application programming) is primarily enamored in Ruby/Python/PHP (Python and PHP being my primary marketable skills) so I would probably need to build my own startup or work for a company that uses the language (it wouldn't be in web application programming though!).
By the way, HN is built using a dialect of LISP based off of Common LISP (is that correct?) of which Scheme (Scheme is also a dialect of LISP) is a brother.