Learning a functional language - which one?

Started by
21 comments, last by Jack9 15 years, 11 months ago
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.
Advertisement
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.

</advocacy>

Quote:Original post by Gage64
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?


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).
Quote:Original post by Simian Man
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).

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.
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).
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.
Quote:Original post by Simian Man
There 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.
Quote:Original post by Hnefi
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.


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.
Quote:Original post by Telastyn
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.


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?
I would assume so, but have no real evidence to support such a conclusion.
A somewhat related question...

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

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.

This topic is closed to new replies.

Advertisement