

Ask HN: Why and how should someone learn functional programming? - dsc

A lot of people tell me that it's the holy grail of programming crafts -which I don't see. So enlighten me.<p>Thanks in advance.
======
newhouseb
In terms of day to day programming, I've picked up the following habits:

* 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

~~~
viraptor
Examples I completely agree with.

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...

------
Ixiaus
I got my first taste of functional programming with Erlang. Honestly, it had
me befuddled for quite some time with pattern matching and the "functional"
style; I was used to using objects to encapsulate rather than "modules" and
"functions" - the distinction in nomenclature may seem frivolous, but the
concepts are worlds apart when used in the context of Erlang or Java.

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.

------
bjg
If you've really never had any functional programming experience and you are
interested in haskell, I would recommend "Learn you a Haskell For Great Good"
<http://learnyouahaskell.com/>

It's a little bit silly, but it's a great bottom up tutorial of haskell and a
lot of functional programming paradigms.

------
vault_
I wouldn't say that it's the holy grail, but there are several reasons why
it's good to know, and also many why we'll probably start seeing a lot more of
it.

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).

~~~
dsc
Any resources you suggest, just off the top of your head?

Realworld haskell is free online, any others?

~~~
vault_
I'm a fan of Learn You a Haskell for Great Good
(<http://learnyouahaskell.com/>). It's not quite done (a couple of advanced
topics are absent at the moment) but it's a good introduction to the language,
and is somewhat less dry to read than your average programming language book.

------
shahriarhaque
I wouldn't say it's a Holy Grail or Magic Wand, or whatever you want to call
it. I would say: learning a (pure) functional language, or taking a university
course on it, is a religious experience. One that every programmer should go
through to learn a new way of looking at problems.

~~~
Andys
Important side note: religious experiences are not always good :-)

------
gtani
I thought these papers are useful:

<http://www.cs.utexas.edu/~wcook/Drafts/2009/essay.pdf>

[http://www.ccs.neu.edu/home/matthias/Presentations/ecoop2004...](http://www.ccs.neu.edu/home/matthias/Presentations/ecoop2004.pdf)

[http://www.bluebytesoftware.com/blog/2010/03/01/OnHaskellTyp...](http://www.bluebytesoftware.com/blog/2010/03/01/OnHaskellTypeClassesAndCInterfaces.aspx)

[http://www.harukizaemon.com/2010/03/functional-
programming-i...](http://www.harukizaemon.com/2010/03/functional-programming-
in-object-oriented-languages.html)

------
daleharvey
it isnt the holy grail, I can say I am a better programmer thanks to learning
erlang though, half of that is due to its functional nature, half due to the
erlangs idioms.

you learn the same way you learn any other language, just pick something to
program and go for it.

------
pdelgallego
I learned using Elisp, it didn't make a better programmer, but I am much more
pragmatic now.

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.

~~~
smanek
Elisp isn't really functional ... everything is dynamically scoped so you
don't get closures and the entire language is built for mutating state.

------
pavelludiq
Referential transparency makes things so much simpler, there is so much less
stuff to keep track of, when your program is much less prone to random state
in one part of your code affecting another.

Its not the "holly grail of programming", its more like "the less broken type
of programming".

~~~
dsc
I think I'm running out of ideas as to where I would IMMEDIATELY want to use
it.

A list of neat projects that should highlight the use of functional
programming would be useful in this case.

------
hga
How long have you been programming and in what languages?

~~~
dsc
For about 6 years. In C, Java, Perl, Python, C++ and, sadly, VB.

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.

~~~
hga
Once you have enough scars from side effects, especially if you try your hand
at concurrent programming, you'll appreciate it a lot more I suspect. Although
as others have noted, it's not exactly a "holy grail".

------
MisterWebz
I'm curious as well.

