Learning a functional language - which one?

This topic is 3562 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I've read many posts here saying that knowing a functional programming language will make you a better programmer, so I'm thinking about trying to learn one. The question is, which one? The ones that seem to be mentioned the most here are Haskell, Scheme, O'Caml and Lisp. Some other names I've heard are Clos, Ruby and F#. In terms of resources, it seems like Scheme would be the best choice. The book "Structure and Interpretation of Computer Programs" is freely available on the internet, and many people on Amazon seem to think it is absolutely revolutionary (though many people there also think it's a piece of junk). There's also a workshop here on GDNet that's based on that book, and there are even free video lectures from Berkley for a course based on that book. I've also read many posts that say that Haskell is very elegant compared to many other functional languages, but there might be less (free) resources for it? What would you recommend and why? Thanks in advance.

Share on other sites
Speaking from experience, Lisp is a very nice language for learning functional programming. When I say "Lisp", I mean both Common Lisp and Scheme - they are both Lisp dialects and either choice will serve you well.

Haskell is also a nice choice, but it's a bit tougher to learn, mostly because it forces you to use functional programming whereas Lisp lets you get away with some shortcuts. The one thing I don't like about Haskell is that it forces you to structure your tabs in a certain way.

As far as resources go, Lisp is definitely the better documented language. There is also no shortage of Lisp compilers/interpreters and even libraries to use Lisp as a scripting language together with, for example, C++. Haskell is more limited in that regard.

Share on other sites
Quote:
 Original post by Gage64...and many people on Amazon seem to think it is absolutely revolutionary (though many people there also think it's a piece of junk).

I don't have much experience with functional languages but they do tend to be quite polarising. You either love it or you hate it!

Anyway I studied some Haskell at university and it was interesting - though I'm not sure it made me a better programmer. Otherwise I've heard good things about Scheme.

It's interesting that you mention F# - I don't know how much of a serious functional language this is but having the .NET libraries available means you can write more interesting programs (and therefore be more motivated) than with the other languages.

Share on other sites
Going with Haskell would be jumping off the deep end. It's a pure functional language, which means that simple things you're used to, like creating a storage for random read/write access (i.e. a simple variable in imperative languages), become complicated.

Haskell, and ML-variants (SML and O'Caml) are statically typed, Lisp and it's variants (Scheme) are dynamically typed. That is a very important distinction that significantly affects the overall programming style.

Lisp programs are actually lists that can be manipulated by Lisp programs themselves, allowing many very interesting and (excessively) clever techniques, at the expense of a somewhat cumbersome syntax.

I haven't tried all of the languages you listed but my suggestion is to start with O'Caml. Besides being a functional language, the static type system is just fascinatingly powerful. And the learning curve shouldn't be too high. You should have no particular difficulties in writing simple programs straight away, once you get the hang of the syntax.

Share on other sites
You might be interested in this:

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

Share on other sites
Quote:
 Original post by CromulentYou might be interested in this:http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-001Spring-2005/CourseHome/index.htm

Thanks. If I go with Scheme, I will probably use the Berkley course because it has free video lectures, but the notes at that link might be a nice addition.

I probably should have mentioned that I am not new to programming in general and am fairly proficient with C++. Does that change anything regarding which language I should pick?

Thanks for your thoughts everyone, and keep 'em coming!

Share on other sites
I just discovered that the book "The Haskell Road To Logic, Maths And Programming" is also freely available online (though some sites only list sample chapters. I hope it is legally free?)

This book seems to teach Haskell in conjunction with various math topics. It seems to get good reviews on Amazon, but I have two things that potentially bother me with it.

First, because of the focus on math, the book probably doesn't go in-depth about the language and it's advanced features. It also means that the scope is very narrow (for example, there won't be code examples that show how to create a simple interpreter, which I've heard functional languages are very suited for).

Second, the topics seem to coincide with those covered in a Discrete Math course, which I already took (I'm a CS student, 1st year). I can't say that I have a good grasp of them though, and we didn't devote a lot of time to Logic (which was the subject that interested me the most), but this book also seems to cover it in just one chapter.

What are your thoughts on this book? Would it be better to use a book that is more "programming-oriented"? Can you recommend such a book? (It doesn't have to be about Haskell.)

Share on other sites
Some of the comments on O'Caml got me interested so I decided to do a google search on it. Interestingly enough, the 4th result was: O'Caml Language Sucks. Any thoughts?

Share on other sites
Well I just finished a C++ class and am also looking to learn a functional language and this free online book caught my eye since it builds a BASIC compiler in one of the chapters!
Computer Science Logo style
I skimmed through some of the chapters and I really liked what I read.
Plus I like the idea of learning Lisp without all the parentheses which really turned me off to learning it when I first heard about it!
"Logo is a computer programming language used for functional programming. It is an easier-to-read adaptation and dialect of the Lisp language; some have called it Lisp without the parentheses. Today, it is known mainly for its turtle graphics, but it also has significant facilities for handling lists, files, I/O, and recursion." -Wikipedia
Here's a sample program in Logo that should impress you
Here is a short but complete program in Berkeley Logo:

if emptyp :menu [print :sofar stop]
end

And here's how you use it. You type

choices [[small medium large]
[vanilla [ultra chocolate] lychee [rum raisin] ginger]
[cone cup]]

and Logo replies

small vanilla cone
small vanilla cup
small ultra chocolate cone
small ultra chocolate cup
small lychee cone
small lychee cup
small rum raisin cone
small rum raisin cup
small ginger cone
small ginger cup
medium vanilla cone
medium vanilla cup
medium ultra chocolate cone
medium ultra chocolate cup
medium lychee cone
medium lychee cup
medium rum raisin cone
medium rum raisin cup
medium ginger cone
medium ginger cup
large vanilla cone
large vanilla cup
large ultra chocolate cone
large ultra chocolate cup
large lychee cone
large lychee cup
large rum raisin cone
large rum raisin cup
large ginger cone
large ginger cup

The program doesn't have anything about the size of the menu built in. You can use any number of categories, and any number of possibilities in each category. Let's see you do that in four lines of Java!

p.s. I wouldn't consider Ruby a functional language as I wouldn't consider Python a functional language. It has some stuff for functional programming but that's it.

[Edited by - daviangel on May 25, 2008 5:09:13 PM]

Share on other sites
I've never understood why people complain about the abundance of parentheses in Lisp. Together with parenthesis highlighting, it makes the structure of your programs extremely clear. This is especially critical in Lisp, where everything (even programs themselves) are lists which consists of cons-cells; this allows some extremely powerful programming techniques but would not be possible without an unambiguous way to tell statements apart.

Share on other sites
Here's the same in O'Caml, with comments:
let choices menu =      (* declare function 'choices'                             type of 'menu' is inferred to be 'string list list'*)    let rec helper menu sofar = (*declare recursive nested function 'helper' with two arguments *)        match menu with        |   [] -> print_string (sofar^"\n")     (* match 'menu' to an empty list *)        |   head::tail ->                       (* match to a list with at least one element *)                List.iter (fun item -> helper tail (sofar^item^" ")) head                    (* Function List.iter is called with an anonymous function                       as the first argument to recursively print out the menu choices *)    in helper menu "" (* Invoke the helper function *);;choices [["small"; "medium"; "large"];    ["vanilla"; "ultra chocolate"; "lychee"; "rum raisin"; "ginger"];    ["cone"; "cup"]];;

The above example shows the use of pattern matching (not the same thing as regex), which is one of the most powerful features of O'Caml. A longer example would be needed to properly highlight why it's so useful.

Share on other sites
If you want a language that will not only teach you a lot, but provide a good practical experience, Ocaml wins. It has probably 85% of the expressivity of Haskell, but also good support for procedural and object-oriented programming as well. Because, as good as the functional style is, it isn't the end-all be-all of programming that some make it out to be.

It also has a fantastic implementation with an interpreter, byte-code compiler, and very efficient native code compiler (check out how well it did in the Computer Language Shootout.

There are also a fair amount of free resources. I'd start with this free book, or this tutorial. Later check out this book.

Quote:
 Original post by Gage64Some of the comments on O'Caml got me interested so I decided to do a google search on it. Interestingly enough, the 4th result was: O'Caml Language Sucks. Any thoughts?

Most of his complaints seem to be that Ocaml is not Lisp. For example, he complains that Ocaml lacks some Lisp features like macros and something called places. He also seems to be trying to use Lisp idioms in his Ocaml progrmas like his with-open-file example.

The only things he mentions which really bother me are the "Record field naming hell" and the lack of overloaded arithmetic operators (which I would rather go without than lose the static type inference).

Share on other sites
Quote:
 Original post by Simian ManThe only things he mentions which really bother me are the "Record field naming hell" and the lack of overloaded arithmetic operators (which I would rather go without than lose the static type inference).

Type inference in SML works fine with overloaded operators for float and int math. My guess is that the O'Caml developers just didn't think that it was worth the trouble to implement. The record fields thing can be a bit annoying, but it is an understandable consequence of how the type system works. I suppose it could easily enough be fixed by requiring ambiguous field names to be prefixed with the name of the datatype defining them.

Share on other sites
Definitely look at Haskell. Along with Clean and Miranda, it's one of the few purely-functional languages available. If you're coming from a purely C-family background, you may find it a little difficult to pick up though. If this is the case, you might want to look at Lisp first to help you pick up the most basic functional concepts (although to say that Lisp is a functional programming language is really missing the point of Lisp).

Share on other sites
There are also a few excellent resources online for Common Lisp:

Practical Common Lisp - http://gigamonkeys.com/book/
On Lisp - http://www.paulgraham.com/onlisp.html (best read after getting a little experience with lisp)

I'd consider learning both an ML-family language and a lisp. They are quite disparate, and both have interesting and powerful constructs that are worth learning.

Share on other sites
Quote:
 Original post by Simian ManThere are also a fair amount of free resources. I'd start with this free book, or this tutorial. Later check out this book.

Thanks for the resources! I will definitely take a look at them.

Share on other sites
Quote:
 Original post by HnefiI've never understood why people complain about the abundance of parentheses in Lisp. Together with parenthesis highlighting, it makes the structure of your programs extremely clear. This is especially critical in Lisp, where everything (even programs themselves) are lists which consists of cons-cells; this allows some extremely powerful programming techniques but would not be possible without an unambiguous way to tell statements apart.

But in common cases, they're extraneous cruft. And in the majority of the other cases the syntax could be changed to something else (like most every other language does) to make the parens extraneous cruft without losing anything. Though the remaining teeny tiny cases, Lisp is sweet.

That said:

I would focus on Haskell, Lisp and F# (not necessarily in that order).

Lisp is very small, and is good/interesting to read through. It best describes the list constructs common in functional programming. I probably wouldn't actually practice much if at all with it.

F# is (mostly) OCaml with .Net access. With the libraries and with a Visual Studio style editor is (imo) the best to transition to and practice with.

Haskell is most noteworthy for its type system which is sweet.

I personally found Scheme obtuse and difficult to get into.

Share on other sites
Quote:
 Original post by TelastynF# is (mostly) OCaml with .Net access. With the libraries and with a Visual Studio style editor is (imo) the best to transition to and practice with.

That kind of makes it sound more attractive than O'Caml, but the books linked by Simian Man look really cool (especially the second one!) and they use O'Caml, so I think I will go with O'Caml for now. I suppose that if I learn O'Caml, learning F# should be pretty easy?

Share on other sites
I would assume so, but have no real evidence to support such a conclusion.

Share on other sites
A somewhat related question...

Has anyone got any experience using the Clean programming language [wikipedia; homepage]?

John B

Share on other sites
Quote:
 Original post by TelastynAnd in the majority of the other cases the syntax could be changed to something else (like most every other language does) to make the parens extraneous cruft without losing anything.

That's just plain wrong. Lisp is built on s-expressions and the ability to manipulate them in a very fundamental way - replacing them with conventional syntax would give you a new language, and it would be a much less interesting and powerful one.

You might not like the syntax; that's fine, many don't. But changing it would be a disaster.

Share on other sites
Right, changing lisp would make it not-lisp, and indeed not very interesting.

Share on other sites
I learned Erlang and was able to read Lisp from that experience. The biggest problem is not which language, but what do you want to do with it, because most functional languages simply don't have the basic 3rd party functionality that comes with most "contemporary" languages. Database connectors/abstractions, enterprise level IDEs (for team development), scaleable/efficient OS level functionality (depending on your needs), and non-native runtimes. Try using Python as your frontend and <insert FunLang here> as your backend.

Erlang, which I stress is my _favorite_, is relatively worthless for anything other than a standalone or clustered Erlang app, unless you are familiar with writing your own low-level drivers, which you have to do IN Erlang. The *nix Erlang interpreter has a number of bugs, forcing you to develop on windows. Although I can't say who has the best 3rd party support, I guarantee it's not Erlang.