views:

3607

answers:

24

One of the blogs I read has been going over the fundamentals of Functional programming lately, and it's gotten me a bit more interested. Then, someone posted a question here that seems like a good fit for recursion/functional programming, which got me thinking about Functional languages again. I'd like to learn one, but now I need to pick one to start learning.

The main Functional languages I hear about are:

  • F#
  • OCaml (and other ML variations)
  • Lisp/Scheme

I used SML in one of my courses in school, and Lisp in a couple others, but I don't really remember much about them at all. I figure learning any one of them will make picking up the others much easier, so it might not matter too much which language I start with, but maybe there's one that's easier to pick up or more generally useful.

I know F# integrates nicely with the rest of .Net, but I'm don't really use .Net at all (I've done a small amount of C#, but that's it). There's Scala, which integrates with the JVM, but my understanding is that it doesn't support some "functional" constructs like tail-call optimization.

So, I figure I'll ask the cloud. What functional language would you recommend I try learning first? What are the pros and cons of the various options?

+2  A: 

Good ol' javascript

Check this out

Al
While Javascript does allow for some functional constructs (mainly Functions as first-order objects), I don't generally think of it as a Functional language. AFAIK it doesn't support things like tail-call optimization and pattern matching.
Herms
That doesn't mean it is a bad choice. Learning a new style of programming in a familiar language may be easier than learning a new style and language together. Plus it will be easier for you to see how to apply the techniques in your daily life.
A 'pure' language is better for learning a paradigm, because it doesn't let you cheat accidentally.This is why my uni chose to teach OO using Eiffel rather than C++ (Java was a bit newfangled in those days)
slim
What's wrong with cheating? ...maybe that that's the Engineer in me, ...or maybe the Gambler, ...or maybe the Hippie. Maybe I just want to win and I'm not very pure.
Al
@Herms: I agree but Lisp, Scala and Clojure also don't support tail calls and many people consider them to be "functional" as well.
Jon Harrop
LISP doesn't support tail calls? JVM languages may not because of the limited jump size in the bytecode and the fact that it breaks exceptions and security, but the claim against LISP in general is just silly!
wowest
+59  A: 

I would recommend learning Haskell, as it's a pure functional language vs. the mixed paradigm languages you mention. Once you get the concepts in your brain, you can then branch out in whatever direction interests you.

Ben Hoffstein
Agreed. It is too easy to slip into an imperative style of programming, and if your language choice allows for that, then you wont get much of a benefit at all.
Jonathan Arkell
Well, the community seems to have chosen you as the best answer (and the one other highly voted answer agrees with you). So, you get to be the "official" answer. :)
Herms
+19  A: 

Scheme is by far the simplest. There are many free Scheme interpreters for various platforms out there, and there are many books on Scheme. Check out the course on Structure and Interpretation of Computer Programs on MIT's Opencourseware.

Dima
I also recommend the Little Schemer.
Terhorst
Dr. Scheme is a good way to get started. It helps with the parenthesis. :)
TURBOxSPOOL
A: 

Nemerle

Even though .net/mono, in addition to standard functional stuff it provides some unique concepts like

  • syntax macros for meta programming
  • seamless mixing of procedural and functional code
pointernil
Those concepts are hardly unique to Nemerle.
Jon Harrop
+7  A: 

I would say scheme first. And only because say you want to learn a functional language. Scheme has the advantage of an excellent online text book that teaches you not just about scheme but about how to think in a functional language. The book is available here http://mitpress.mit.edu/sicp/.

After you go through the book, it should be much easier to learn whatever functional language you want like Haskell, Common Lisp, F#, etc.. But a functional language is kind of useless if you try to program C/C++ in it. So anyway use the free online textbook to learn Scheme. Then if scheme isn't enough for you go on and learn other ones.

Cervo
+24  A: 

Haskell is a modern, statically-typed, lazily-evaluated, pure functional language, and I would recommend it for really learning functional programming. Haskell tends to force the programmer to write code in a functional style, since attempting to write code in an imperative style is rather difficult in Haskell.

Note that F# is essentially a .NET implementation of O'Caml and Scheme is a language in the LISP family. JavaScript has some elements of functional style programming, but it is missing a lot. All these languages are open to both functional-style and imperative-style programming, so it is easy to slip back to imperative style instead of really learning to use functional style programming.

Justice
+2  A: 

Haskell's interesting point is it's type system. It has strong typing and can infer types for all functions automatically. And I think it's really easy to get started, see for example a factorial function (the hello world of functional languages ;)

fact 0 = 1

fact n = n * fact (n - 1)

Also there's LISP. I don't know much about it, but from what I've heard, it's a legendary language.

Roman Plášil
it should be fact n = n * fact (n-1) ;)
Thomas Danecker
+9  A: 

I recommend Scala a great programming based on the Java VM. It's a successful mix between imperative and functional programming.

Steve Gury
I looked at scala. I might be interested in using it eventually, as it could be used along with our Java codebase, but for now I'm looking for a more pure Functional language.
Herms
I really hope scala takes over java eventually...
CrazyJugglerDrummer
+3  A: 

I'd recommend Haskell, because it has a nice free interpreter, it's lazy, and does type inference for you if you want. It's very nice for a starting language, although I think using GHC you can pretty much do any kind of programs.

dguaraglia
+3  A: 

F# is a great functional language.

As I stated here:

F# is a fantastic static-typed functional language with phenomenal type inference eliminating a lot of the code bloat often associated with statically-typed languages. It also has access to the .net libraries which are extensive and very well designed, and being multi-paradigm its flexible enough to allow you to use imperative programming when required. It really is one of the best languages out there at the moment! :-)

kronoz
+14  A: 

Erlang is a functional language, which is not only functional, but also concurrency oriented. It is supposed to work very efficiently with multicore processors (and that seems to be the future). So if you go with Erlang, you can kill two birds with one stone :)

It does not have the parentheses (that Lisp has) and does not have the lazy evaluation (that Haskell has). But it has all the other "functional stuff", like tail recursion, pattern matching, functions as first class citizens, lambda expressions.

hcs42
Erlang also lacks typing.
Apocalisp
Erlang has dynamic typing, i.e. the "objects" do have types, but the typing is performed at run-time. (Just like Perl, Python, PHP, Lisp, etc.)
hcs42
Erlang is a great language to learn eventually, but I wouldn't recommend it as a first functional language. You'd be learning functional programming and parallel programming at the same time, which in my opinion is a little much. Learn Haskell or Lisp first.
musicfreak
Erlang can have static type analysis through type inference with the help of TypEr and Dialyzer.Better than that, they can even analyze already compiled code you wish to integrate with and find errors in running applications.http://erlang.org/doc/man/dialyzer.htmlhttp://www.erlang.org/eeps/eep-0008.htmlhttp://www.erlang.se/workshop/2005/TypEr_Erlang05.pdf
I GIVE TERRIBLE ADVICE
A: 

lisp is nice

Abhishek Mishra
+2  A: 

Scheme is a great start, once you start thinking functionally you can pick up another functional language relatively easy with a bit more effort.

PLT Scheme / Dr. Scheme is a good ide/interpreter/compiler.

Jim Ford
+2  A: 

XSLT is another doorway of insight into functional programming.

While XSLT is arguably not actually a programming language per-se, it certainly provides several of the functional idioms such as variables are immutable after creation, functions/templates cannot have side effects, etc.

David Alpert
XSLT is not especially nice. I don't recommend it for any purpose.
Marcin
XSLT is great! I recommend it for many purposes.
Al
XSLT is great for what it's great for. What distinguishes it amongst functional languages (for which I agree it barely qualifies) is that it's on every computer. Even IE6 runs it.
harpo
I have found that wrapping my brain around the immutable and side-effect-free parts of XSL has had a huge impact on how I think about structuring code and writing methods.
David Alpert
+3  A: 

OCaml has a very nice balance of functional and imperative features, some decent libraries, and reasonable performance.

Chris Conway
+1  A: 

I was taught Miranda (for its own sake) as an undergraduate, and it didn't make a lot of sense. Haskell and Miranda are pretty similar, I gather.

Then a term later I was taught Lisp (for the purposes of AI). I think the reason this went so much better was more about the tutor than the language.

When I returned to Miranda, it all made sense in light of what I'd learned about Lisp.

Having said that, if you can find the right educational resource, I feel that a language like Haskell with more syntactical sugar, ought to be easier to learn than Lisp.

slim
I'm just now starting to play with Miranda and I think it might be good for someone with a bit of mathematical background but not much programming experience. It's kind of a simpler version of Haskell.
Barry Brown
+3  A: 

I suggest you to take a look at Erlang. I read Programming Erlang - Software for a concurrent world by Joel Armstrong and get hooked with Erlang in the first four chapters.

JAG
+4  A: 

Definetly Erlang. It's powerful and simple. Might not be the most pretty but most functional languages aren't. Quick to install within 5 minutes you will be writing your hello world.

This is just my opinion but I found it to be the simplest to get started in also.

Jon Gretar
+1  A: 

A less "pure" answer, and one that lets you play with functional constructs in a very limited way in a familiar environment is to begin by working with the functional constructs in a mostly procedural language (e.g. python).

When I initially ran into this stuff in python I had no idea I'd run into something significant enough to have it's own title, it just seemed like the language supported this cool and elegant way of writing that I'd never seen before (map and lambda constructs, for example). After that lisp didn't seem too alien.

Steve B.
A: 

C++, Smalltalk, and python.

That's right - you can use functional programming techniques in all of those languages.

I wouldn't recommend them as your only functional programming languages, because using languages focused on functional programming helps inform taste. In the end the choice between FP-oriented languages is minimal if you just want to learn. Common lisp is the least clean, though.

Marcin
A: 

I'd suggest you start with programming functional programming in Python. Okay, so it isn't purely functional, but no one is forcing you to utter the word "class" in your code :) You should be looking at inner functions, dynamic scoping, lambda, comprehensions, returning functions as values, and map/filter/reduce. Mainly, try to write code in Python which doesn't spend lots of code writing values to modules or class instances, and is preferably full of lots of nice little functions you can reuse in later projects.

Disadvantages: There are some functional programming concepts which Python simply doesn't have; static type inference and pattern matching being the big ones. You may also find the syntax clunky compared to, say, ML languages. Python isn't that close to the metal, but then you do have the struct and socket and os modules which you can use as necessary. But, Python is a pragmatic choice for many purposes so, with the huge library, you'll actually be able to write lots of code.

I'm afraid I've not read any good literature on functional programming in Python, but it does have most of the useful stuff.

Dickon Reed
A: 

I'm currently learning Haskell to get to grips with functional programming but I'm trying to do a little bit of Erlang on the side. From reading the previous answers my experience seems to be fairly typical:

I too read Programming Erlang by Joe Armstrong and fell in love with it. But from this I learned about Haskell. I have decided to stick to learning Haskell to get my brain used to doing functional programming and then I'm going to move onto Erlang - which is where I want to end up ulitmately.

I don't think there's any harm in dipping into other functional languages just to have a play around. I've already ordered a book on Clojure and I'm thinking of ordering an F# one as well (If someone can recommend one i'd be greatful).

MattyW
A: 

I still can't believe that not one person mentioned about Lua. This language has really simple syntax, gets the basics right and it is functional at core. Executes really fast whatever you throw on it.

Also it has everything you need to get you into the OOP path later on. Good range of modules and a really helpful community. Those sum it up for Lua.

petsagouris
Lua isn't a functional language.
Don Stewart
@Don so what is it ?
petsagouris
A: 

You can try almost all languages mentioned here at ideone.com

kuszi