Ask HN: which functional language to learn?

30 points by vijayr ↗ HN
I'm looking at learning a functional programming language, but not able to decide which one. The language should be fun to learn and also have decent job opportunities.

Based on some initial reading, Haskell and Erlang seem to be good choices. Can anyone suggest a language?

My background (not sure if it matters) I've worked with Java/PHP/Ruby, developing desktop and web applications.

47 comments

[ 7.0 ms ] story [ 173 ms ] thread
I'd suggest Scheme, just because the SICP videos make learning it easy and fun (http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussma...). And, once you get the functional concepts, Haskell and/or Erlang should be relatively easy.
I think this is a good idea to first learn the basic functional concepts using Scheme and the SICP videos (heard a lot of good things about the videos and the book). I'm learning Haskell (I'm a C# guy) and some things are a bit difficult to understand if you have an object oriented background. I think I'll have a look at the SICP videos, too
Did you learn Scheme and then moved to Haskell, or directly started with Haskell?
I directly started with Haskell, but had some difficulties with the functional concepts! this is why I second the idea of michael_dorfman. I think it will help you a lot to understand everything better. and I personally like video lectures a lot. not that I'm lazy and don't want to read, but sometimes, when something is explained visually or by a specialist in the specific field, you just get it better in your head :)
I did find lot of books/resources for Haskell and Erlang too.

If it were the usual programming languages (PHP, Ruby, Python etc), making a choice is relatively easy. For functional languages, I'm not sure where to even begin.

> If it were the usual programming languages (PHP, Ruby, Python etc), making a choice is relatively easy.

What do you mean by that? Is it just that you know this field better and thus feel better informed, or are you hinting at something else, that I didn't get?

I mean this: I know (to some extent atleast) what to expect from languages like Python etc. So if I am trying to choose between say, Ruby and Python, I can use them both for a couple of weeks, and decide which to go with, depending on which I like and am more comfortable with. I also know where these languages are mostly used, job prospects of them etc.

Whereas with functional programming, I am totally new and lost, no clue where to even begin.

No, I didn't hint anything else.

I'd definitely second SICP if you've never done functional before. It teaches you functional concepts almost without you even realizing it.

OTOH, if you are really interested in being truly pure, it's hard to beat Haskell. Real World Haskell is a great intro for that language, and it's free online.

Ruby, used that way, is quite a functional language. I think you might find a lot of similarities to Scheme, which might make it easier to get in to. I know I did (although reversed .. I learned Scheme first).
I think this comes up enough that we should have some sort of FAHN: Frequently Ask(ed) Hacker News.

That said. Scheme with SICP, or The Little Schemer and then SICP

I've got to second The Little Schemer. I ran through it earlier this year, and it was the most fun I've had programming in a long time.
Javascript
Seconded. Although it's a functional language in the same way lisp is (i.e. it isn't).
Javascript is a great language (and widely misused, alas). However, calling it a functional language is a very big stretch (unlike lisp).
Javascript has a lot of Scheme legacy. I'd definitely call it a functional language.
Javascript can be used that way, and some of Javascript technologies suggest that, but it can and usually is used in the mainstream imperative way. People who have not been exposed to functional programming before touching Javascript (which is an overwhelming majority) may not even know about its functional capabilities.
I learned OCaml first, and then switched to F#. Out of all languages I know (OCaml, Java, C#, C++, Python, VB, Lisp) - IMHO F# is the best one. Learning curve is pretty steep though, once you move past the basics.
> Learning curve is pretty steep though

Do you mean it gets difficult, or that progress gets fast?

It gets difficult. F# has pretty much everything from every other functional language. Even though F# is very consistent language. There are a lots of concepts to understand.
No, it has just a few concepts that if taken seriously (i.e. don't try to write imperative/prescriptive code) will lead to the natural discovery of many of its other features (or you'll end up inventing them yourself).
I am talking about continuations and workflows -> these concepts are not that straight forward, especially if you are coming in from imperative programming background.
Fair enough. What I mean is that F# (and Haskell) can be useful without knowing about Workflows (and Monads). By the time you find yourself needing them you'd either invent them (fairly easily) or find them... It's not much harder than INNER JOIN in SQL which I also think most people would eventually invent if the language didn't have it.
Could also be the very new state of it with lack of good documentation.
And the lack of existing code to take cues from. I looked at F# earlier in the year, and this was the thing that tripped me up the most. I can wrap my head around functional programming "in the small" pretty easily, but I'm pretty stumped about how to put together a full application.
Clojure. It's a lisp on JVM.
What about clojure? With a Java background it's easier to grasp, has all the modern features and a friendly, supportive community. It's load of fun, you could even use SICP with it. For job opportunities you need a good load of luck with any functional language. With such a close integration with Java you have a high chance for a stealth clojure project. :-)
In consideration of your background, scala would come to mind. There is some dispute about whether it's a functional language, though.
Haskell is a bit into the weeds if you're just starting out. I jumped in with Erlang, as its pretty pragmatic so the tutorials out there tend to focus on getting crap done and not so much theory.

Once you've written some code and feel comfortable with FP, then drink the Haskell-aid.

Clojure. Without a doubt. It's by far the easiest and most productive Lisp around right now. You get a modern core library with great support for concurrency, and you can use Java libraries with ease. The new Clojure book published by the Pragmatic Programmer guys a pretty good read, and it gets into the important aspects of functional and concurrent programming. I've been having a blast so far.
How big should be the server to run this site (with all its traffic), if it could be ported from ark to Clojure? =)
Notes about Haskell:

It's strange, but Haskell is more difficult to read than write. This is because most code on e.g. Hackage uses some very complicated gymnastics involving Applicative, Arrows, or whatever. So it's hard to learn from other people's programs.

Also, a warning: learning Haskell might screw with your ability to write code in other languages! It took me two weeks before I stopped making every line of Python a lambda.

I'm really curious to see how other people are progressing through the learning process for Haskell (and other HM-based functional languages). I rarely use lambdas, and I feel that Haskell is much easier to read than to write. Clearly there's something different about our approaches. I learned SML back in school, picked up OCaml about 3 years ago, and picking up Haskell was still a bit of a trip.
I also find I use lambdas less in Haskell than in other languages. I think it's because in Haskell, constructs like partial application, function composition or lifting are concise and natural to express, and those constructs cover most common use cases for an anonymous function. I think

map (find needle) haystacks

is clearer (as well as shorter!) than

map (\haystack -> find needle haystack) haystacks

In most other languages things like partial application are a pain to express, so a lambda is an easier way out.

(Now I'm imagining the six lines of Java I'd have to write to achieve the same effect as those four words of Haskell, and trying not to giggle. Functional style is addictive.)

Sorry - I meant that it makes me use lambdas in Python (you're right, I don't use many lambdas in Haskell either).

I find myself using them in Python everywhere as a substitute for currying and/or laziness, e.g.:

  def f(a,b): 
    ... # do stuff
  def g(a,b):
    ... # do other stuff
  meths = {'use f':f, 'use g':g}
  a = calculate_a()
  m = pick_a_method()
  x = do_cool_thing(data,lambda b: meths['m'](a,b))
Definitely Haskell. May I add one more resource for learning it that worked great for me, the official "Gentle Introduction to Haskell":

http://www.haskell.org/tutorial/

For those already proficient with programming that just wnat to pick up Haskell, it's ideal.

Then I would suggest an exercise that involves manipulating abstract syntax, for example parsing and interpreting a little language. ("Build yourself a Scheme..." might be good though I didn't read it myself.) This is where the language really shines, and you don't need any of the recently popular trickery like arrows or GADTs.

In my opinion don't learn any LISP, Common LISP or Scheme, it doesn't matter. Why? Fragmentation. It's very hard to write something a bit complex that can run on the various LISP compilers/interpreters without heavy changes. Also, there is a lack of good and up-to-date 3rd party libraries/frameworks which leads to having to write things that most languages already provide by default or than can be easily obtained.

Go with Scala, Clojure or Haskell. The first two run over the JVM and seem to have a very nice community behind them. Haskell is very cool and it will definitely change the way you look at programming.

There is also F# which is very cool and runs over the .NET framework. If you're into Microsoft stuff then it might be worth it.

Well, technically Clojure is a lisp, but it does indeed depart from the heavyweight lisp legacy as represented by Common Lisp. And portability is much easier when all your libraries are in JVM. :-)

Scala started earlier than Clojure and has a bigger community around it. Clojure is clean and grows quickly, it has a very good potential. Besides, it has a strong and multifaceted support for parallel (multicore) programming. Both Scala and Clojure run on top of JVM and interop with it.

Haskell has had a strong following in academia for quite a while now. Newer languages borrow from it.

I have not tried F# which, as far as I understand, is a kind of ML on .NET. Its community seems to be smaller, but it may be due to the usual animosity some people feel regarding anything that comes out of Microsoft. Reportedly the language is well designed.

Maybe F# acceptance will grow when Visual Studio 2010 comes out, since it will be one of three new native languages: IronPython, IronRuby and F#.
It may happen, but it would compete with the well established Ruby and Python (in addition to C# and VB). Anyway, I am glad that they will be added to the bunch.
I recently asked myself the same question, and I ended up settling on SBCL (Common Lisp).

Emacs coupled with SLIME provides the best development environment I have yet experienced. For me, the greatest downside is that the code I end up with is often quite system-specific.

I too settled on SBCL for a while, but then looked around a bit wider. Clojure is an intriguing development. I recommend that you look at it, both the language and the community. It has a good promise, borrowing the best from the older lisps and leaving behind the unnecessary legacy.

So, unless you need to support or interact with past Lisp code, I think Clojure may be a better tool for most of us. YMMV, as usual.

As I'm sure you know, functional programming is a style and you can use it for any of the languages you mentioned (yes, including Java - objects are a poor man's closure but closure's are a poor man's object) [1]. One thing that might help is to try using functional idioms in the languages you know. Then you'll appreciate the syntatic help that "real" functional provide for that style.

[1]http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/m...

One more thing that you may or may not know is that functional programming languages are a kind of fuzzy concept. There are at least two different categories (well, probably it's more a continuum, but ..), being functional programming languages vs. function-level programming languages. (Note: Someone please correct me, if I'm confusing the terms here). Lisp's belongs in the first category, where Haskell probably belongs in the latter. The first category is more similar to main stream, procedural languages, where the latter is more abstract. Which is a better choice is open for debate; On the one hand, you'll probably grasp Lisp faster than Haskell, on the other hand you might end up writing procedural code in it, instead of function oriented code.

See: http://en.wikipedia.org/wiki/Function-level_programming#Cont...

I work for a "microsoft shop." This means that if there is an MS and a non-MS choice of technology, we'll always take the MS one. Consequently, F# is the only possible choice for a functional language at work.
I too work for a Microsoft shop. Installed Emacs, perl, MinGW/MSys with bash, cygwin (do not use it, though), python (do not use it), etc. JVM is already installed on all computers except the production servers. So the work that is of a "personal" scope (editing a file, analyzing a log, etc) I can use any technology which is available to me.

Of course, when we are talking about a group effort, we need a technology that everyone in the group is understanding. It is more difficult to convince others (including management) in a Microsoft shop to use non-Microsoft technologies. But it is possible.