share
Stack OverflowWhat's a good Functional language to learn first?
[+62] [24] Herms
[2008-09-19 15:31:31]
[ functional-programming polls ]
[ http://stackoverflow.com/questions/102911/whats-a-good-functional-language-to-learn-first ] [DELETED]

One of the blogs [1] 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 [2] 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:

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?

[+77] [2008-09-19 15:35:11] Ben Hoffstein [ACCEPTED]

I would recommend learning Haskell [1], 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.

[1] http://www.haskell.org/

(5) 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
(2) 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
1
[+27] [2008-09-19 15:44:41] yfeldblum

Haskell [1] 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.

[1] http://www.haskell.org/

2
[+23] [2008-09-19 15:39:45] Dima

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 [1] on MIT's Opencourseware.

[1] http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-001Spring-2005/CourseHome/index.htm

I also recommend the Little Schemer. - Terhorst
Dr. Scheme is a good way to get started. It helps with the parenthesis. :) - Mr. Shickadance
Edwin comes with MIT Scheme, for any EMACS-ers out there. Kinda like Python's IDLE (i.e. line editing), which beats running scheme out of an xterm. - new123456
3
[+17] [2008-09-19 16:08:04] hcs42

Erlang [1] 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.

[1] http://erlang.org

Erlang also lacks typing. - Apocalisp
(1) 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
(2) 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. - Sasha Chedygov
(1) 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. erlang.org/doc/man/dialyzer.html erlang.org/eeps/eep-0008.html erlang.se/workshop/2005/TypEr_Erlang05.pdf - I GIVE TERRIBLE ADVICE
4
[+10] [2008-09-19 15:46:23] Steve Gury

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

[1] http://www.scala-lang.org/

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
(3) I really hope scala takes over java eventually... - CrazyJugglerDrummer
5
[+8] [2008-09-19 15:42:41] Cervo

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.


6
[+6] [2008-09-19 15:55:36] kronoz

F# [1] is a great functional language.

As I stated here [2]:

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! :-)

[1] http://research.microsoft.com/fsharp/fsharp.aspx
[2] http://stackoverflow.com/questions/87212/whats-the-best-statically-typed-language-for-a-dynamically-typed-language-progr#87232

7
[+4] [2008-09-19 15:52:03] dguaraglia

I'd recommend Haskell, because it has a nice free interpreter [1], 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 [2] you can pretty much do any kind of programs.

[1] http://www.haskell.org/hugs/
[2] http://www.haskell.org/haskellwiki/GHC

8
[+4] [2008-09-19 18:54:36] Jon Gretar

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.


9
[+3] [2008-09-19 15:45:25] Roman Plášil

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.


it should be fact n = n * fact (n-1) ;) - Thomas Danecker
Actually, this is not the best way to do this. I usually write fact n = product [1..n]. - FUZxxl
10
[+3] [2008-09-19 18:43:03] Chris Conway

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


11
[+3] [2008-09-19 18:47:21] JAG

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.


12
[+2] [2008-09-19 15:32:32] Al.

Good ol' javascript

Check this out [1]

[1] http://www.ibm.com/developerworks/java/library/wa-javascript.html

(2) 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. - user11318
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
13
[+2] [2008-09-19 16:14:51] Jim Ford

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 [1] is a good ide/interpreter/compiler.

[1] http://www.drscheme.org/

14
[+2] [2008-09-19 16:46:16] David Alpert

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.


(3) XSLT is not especially nice. I don't recommend it for any purpose. - Marcin
XSLT is great! I recommend it for many purposes. - Al.
(1) 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
15
[+1] [2008-09-19 18:45:28] slim

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.


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
16
[+1] [2008-09-26 01:30:59] Steve B.

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.


17
[0] [2008-09-19 15:40:49] pointernil

Nemerle [1]

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
[1] http://nemerle.org/Main_Page

Those concepts are hardly unique to Nemerle. - Jon Harrop
18
[0] [2008-09-19 16:13:36] Abhishek Mishra

lisp is nice


19
[0] [2009-02-20 18:23:27] Dickon Reed

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.


20
[0] [2009-08-27 12:37:28] MattyW

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


21
[0] [2009-08-27 15:53:59] petsagouris

I still can't believe that not one person mentioned about Lua [1]. 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 [2].

[1] http://www.lua.org
[2] http://www.lua.org

(1) Lua isn't a functional language. - Don Stewart
@Don so what is it ? - petsagouris
22
[0] [2010-07-28 22:24:09] kuszi

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

[1] http://ideone.com

23
[-4] [2008-09-26 17:24:13] Marcin

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.


24