Haskell vs. OCaml--which do you prefer?

by Christopher Roach

Those of you who have been following my blog for any time at all have probably noticed that I tend to write mainly about programming languages, with a recent concentration on Lisp. I love programming languages, I think that goes without saying--although, I've just said it anyway--and I tend to like dynamic (or agile) languages that allow me to concentrate on the problem I want to solve rather than the intricacies of things like variable typing and memory management. For these reasons I have tended to use Python for most of my non-work-related work (or hobbyist type work and school) and I have recently been learning, using, and enjoying Lisp as an alternative. In fact, many times I have found myself falling back on Lisp when I've run into a problem using Python rather than the other way around. It is this new found love for Lisp that has got me wondering what other functional programming languages are out there. In my searches I have come across two candidates for the language I am hoping to learn next--OCaml and Haskell.

I first started playing around with Haskell, mainly because it is the only truly pure functional language that I know of and I really liked some of its features, such as pattern matching and curried functions, but after trying to wrap my brain around it (so far, not completely successfully), I am finding that it's hard to do everything I want to do with Haskell. Plus, it has been slow in a few areas (probably mostly due to my ineptitude, but still, I've been holding out for that holy grail of a language that lets me use recursion significantly in my code with almost no performance hits). All of this led me to look into OCaml. OCaml seems to me like a gentle introduction to Haskell. It has some of the same features, but also includes support for more familiar programming paradigms (i.e., imperative and OO). Also, OCaml seems to knock the socks off of just about any other language outside of C when it comes to performance comparisons--a very good thing since much of my school work/research makes use of some extremely time consuming code.

All in all, both languages seem to have several advantages/disadvantages that make me look at each for a few days and then go running back to the other looking for solace. I prefer Haskell's syntax to OCaml's and I love that Haskell is purely functional, but I am finding that the purely functional aspects are making it tough to do certain things (e.g., quite a few of the algorithms in my Bioinformatics class use Dynamic Programming. I have no idea how this would be done in Haskell and I've tried the recursive way of doing things and it is just way too slow). I also hate that I can't declare a function in Hugs or GHC's interactive shell without doing some really kludgy tricks.

As for OCaml, I do like that I can use imperative features, this would make OCaml a better candidate for everyday use since I could more easily use it for scripting and quick tasks. It seems to have a larger set of libraries as well. All of this would seem to point to OCaml as the better choice for a language I could use for more than just academic tasks. However, I still have gripes with OCaml as well--namely, syntax. I much prefer the syntax of Haskell to OCaml. I like Haskell's pattern matching syntax more than OCaml's, I absolutely hate the double semi-colon (;;) that is used as an end-of-line marker, and I don't particularly like that I have to explicitly state that a function is recursive in OCaml.

All of that said, we get to real point of this post. Basically, I really want to learn another language that moves me even more into the functional programming paradigm. I've enjoyed what Lisp has to offer and it has made me much more appreciative of the power of the functional paradigm, but, frankly, I find myself wanting even more. Because of this desire, I am asking you--my readers, wanderers, visitor's--regardless of how it is you have happened by this blog post, I am leaving it up to you to point out to me the benefits and deficiencies of each of these languages. I am hoping that after reading each of your comments that I will have a clearer picture of the purpose of each of these two languages and I can go about making my choice for the next programming language I am going to learn. So, please, if you have had any experience in either language--good or bad--make a comment below and give me the insight I need to make the best choice for my next language.

Thank you all very much in advance for your comments--they are, as always, very much appreciated.


Darren Chamberlain
2006-03-10 09:38:31
I just recently read a pair of good articles on OCaml: http://www.cabochon.com/~stevey/blog-rants/ocaml.html and http://www.cabochon.com/~stevey/blog-rants/more-ocaml.html.

Have you given scheme a look? I've heard people (including Peter Seibel in Practical Common Lisp) describe it as more purely functional than Lisp, which sounds like it might be right up your alley.

2006-03-10 10:43:04
Both are fine languages, but I think you have some misconceptions about Haskell and need to spend some more time trying to learn it properly before writing it off. I'm not sure why defining a function like this in the GHC toplevel is a kludge:

Prelude> let f x = x + 1
Prelude> f 1
Prelude> let fib 0 = 1; fib 1 = 1; fib n = fib (n-1) + fib (n-2) -- yes, naive definition
Prelude> map fib [1..20]

If you want to define one or two line functions, this works fine, and if they're longer, you should be putting it in a file anyway. (You can still define it in the toplevel, but it'll be a bit awkward.) I think that's pretty common for toplevels in general.

In any case, to really appreciate Haskell, you need to get to the point where you understand monads and type classes. Those are some of Haskell's much more unique and powerful features. I obviously don't have the time to write out a huge essay explaining everything about Haskell (there are lots of resources out there on the web and you should do your own research), but I would recommend checking out #haskell on irc.freenode.net for further questions. It's an extremely helpful channel and they would certainly help to put you on the right track as far as your performance issues go and your confusion about how to do dynamic programming (dynamic programming algorithms should be very natural to write in a functional style in Haskell). They would also likely be willing to explain differences between Haskell and OCaml in much more detail (and many of the people in there will know both quite well). Definitely check it out.

William D. Neumann
2006-03-10 10:46:08
OK, here's my two cents:

Let me start off by stating that I will mainly be discussing OCaml here, as it is my primary language, and I am more familiar with it than Haskell (which I keep noodling with but never really get too deeply into -- despite the nice things like type classes and GADTs that I'd love to see brought over into OCaml).

First off, regarding your two stated syntax issues:
The ;; separator is only needed when signaling the toplevel that it should take what you've given it and interpret it. You don't need it for most of your code, mostly when you want to send something into the toplevel. For example, take a look at the code in the standard library, most files have none, or just a few sets of ;; and those are probably in there from some interactive developmentt session.

Also, regarding the need to label recursive functions as such. Yeah, it's a tiny bit of an annoyance. Of course, it does give you the (questionably useful) ability to do something like:

let f i = if i < 10 then 0 else i

let f x = if x <=0 then 0 else x + f (pred x);;

where f 10 yields 10 instead of 55, as it would were functions assumed to be recursive.

Now, onto what you really wanted to know, the benefits and deficiencies of the languages. With OCaml, you've touched on a few of these already (though you surprisingly left out the + vs +. floating point arithmetic syntax -- this is usually the first thing people mention): OCaml has a very nice interactive toplevel for performing iterative development, which unlike Haskell's doesn't require you to fudge around to enter code. Incidentally, I have written an OS X wrapper for the toplevel (which I really need to get back to work on...) that is a bit nicer than dealing with ledit and the terminal. It also has the flexibility of working in the toplevel, as an interpreter, compiling to bytecode for portability, or compiling to native code for speed (and as noted, the resulting code can be very fast). And there is the whole "multi-paradigm" aspect of OCaml as well, where you can mix and match functional, imperative, and object oriented approaches at will, choosing the best approach to each problem, rather than shoehorning the problem into one single approach.

In addition to those, one of the nicer things about OCaml, particularly for a newcomer to the language, is the amount of material available to help you learn the language. There is the (somewhat out of date, but still very valuable) O'Reilly book translation, there is Jason Hickey's excellent introductory tutorial (PDF), and Richard Jones's OCaml tutorial which is geared towards people coming over from languages like C, Java, or Perl. I've noticed that more introductory texts on Haskell are coming out, but it still seems to be lagging well behind OCaml.

And like everything else, there are the mailing lists, for beginners and more advanced users. Since you seem to still be working your way into the more functional realm, it would probably be much easier to do with OCaml than Haskell.

Now that's not to say there aren't some down sides to OCaml. For one thing, its garbage collector is non-concurrent, so if you want to take advantage of multiprocessing, you will have to launch multiple instances of a program and utilize channels or other message passing means -- threads will all be stuck on the same processor as the spawning program. For another thing, some of the documentation is still a little light (or just not accessible to beginners) for some features of the language, like functors, polymorphic variants, and the powerful camlp4 preprocessor. And, of course, there are some annoyances with the toplevel mostly stemming from the redefinition of types and values without redefining everything it depends on. For example, the classic "This expression has type x but is here used with type x":

# type var = A of int | B of float;;
type var = A of int | B of float

# let f = function
| A i -> print_int i
| B f -> print_float f;;
val f : var -> unit =

# let a = A 100;;
val a : var = A 100

#f a;;
100- : unit = ()

# type var = A of int | B of float | C;;
type var = A of int | B of float | C

# let f = function
| A i -> print_int i
| B f -> print_float f
| C -> print_string "Hello";;
val f : var -> unit =

# f a;;
Characters 3-4:
f a;;
This expression has type var but is here used with type var

And finally, one of the things that you are going to have to watch out for (really with either language) is the extremely strong typing that is at the core of the language -- especially coming from a Python/Lisp experience. The type inferencing makes life very easy in this regard, but you may bristle at the need for functions like print_int, print_float, and print_string, as opposed to a generic print function, which is something that Haskell's type classes can help avoid.

I hope that helps you out a bit (at least on the OCaml side, I don't offer much on Haskell). If you have any further questions, post them here and I'll see if I can help more.

2006-03-10 10:57:08
Ocaml and haskell seem to me as cousins, a la C/C++. When you learn something in one, almost always that thing is useful in the other one.

For a non-functional programmer O'caml is easier to assimilate for its more conventional imperative properties. And being so fast it is a good alternative to C++.

On the other hand, Haskell has one of the most powerfull type system of the languages in use outside-the-lab. So there are thing that are easier to do in Haskell than in O'caml.

Ah! and haskell *do* have imperative features too. In fact, it has embeded a sublanguage for doing things such IO, references and arrays. Yes, it is the IO monad and the do notation. The main difference here is that the do notation is uglier than imperative O'caml, and that, by design, imperative computations are isolated of the functional code.

So, if you enjoy programming in O'caml, and your project can afford a suboptimal process time, then there are chances you can use haskell too.

BTW, if you have a chance, take a look a this book:

The haskell school of expression

After reading it, you won't be the same...

2006-03-10 11:26:12
Have you looked at ML yet? It's very popular among PL researchers and has some very powerful features. It's strongly typed, has pattern matching, and includes imperative features. There are several free versions available. One of the more popular ones is standard ML of New Jersey, which you can get at http://www.smlnj.org/ There are many online tutorials, try http://www.dcs.napier.ac.uk/course-notes/sml/manual.html or see http://www.smlnj.org/doc/literature.html for more links.
2006-03-10 11:26:56
I've attempted to learn both languages, coming from a Lisp background. Haskell is definitely the more interesting of the two languages, and will stretch your mind more than Ocaml. I would say that 40% of the time I spent with Ocaml I was wrestling with its ugly, confusing syntax, rather than learning anything significant. What's more, Ocaml is more similar to Lisp than Haskell is, so there's less to learn from Ocaml. I'm not going to say that Haskell is a better language for doing Real Work than Ocaml, but it is a better language to learn new concepts.

As a disclaimer, I never mastered either Ocaml or Haskell, because I just don't have time to spend on something that won't pay off in the immediate future right now. These are just impressions from spending a week or so with each language, and I probably have misconceptions about both of them, so take it for what it's worth.

2006-03-10 11:29:38
I only discovered OCaml after Haskell, but it has certainly surpassed it as my language of choice. Both are statically typed functional languages, although Haskell is more feature-full (with unique laziness semantics and advanced features like type classes and monads), I feel OCaml is a really nice language and a particulary nice environment. I think the language has just the right amount of features to make it truly useful and expressive, yet not too much to make it too complex to consider in its entirety. (Most people also like that OCaml is open to different paradigms: functional, imperative, object-oriented). With a nice environment, I mean that it comes by default with a lot of useful tools: a good standard library, a toplevel, a bytecode and native-code compiler, a debugger, a profiler, a lexer/parser, a macro-system (camlp4), etc. I regret that your first thoughts on these languages are determined by a property as mundane as syntax. Camlp4 allows you to redefine the syntax...
2006-03-10 11:33:36
Do you have a logical language in your tool kit? If not, consider learning one while you're binging (or if you purge afterward, then while purging) on functional languages.
2006-03-10 18:06:53
First-off, you can't go too far wrong with either language. They're both excellent. I'm an OCaml user, personally, though I certainly wouldn't turn down a Haskell gig. The syntax issues you list with OCaml are really not imporant IMHO. You never use ;; in real code, just in the interactive interpreter. "let rec" vs. "let" is pretty minor, and makes it easier for the compiler to optimize your code. I don't know what you dislike about OCaml's pattern matching syntax -- it's just about perfect as far as I'm concerned. But hey, I find Haskell's formatting rule to be arbitrary and restrictive. Opinions vary on these things.

The real differences between the languages have to do with their type systems and performance. Haskell seems to have the more expressive type system. The language seems to have lots of interesting features like rewrites and "sub-languages" like the monad syntax. This can be good, but it can also make the language pretty hard to understand. OCaml has the edge on performance, both compile-time and run-time. I've seen some pretty short Haskell programs that took surprisingly long to compile. With OCaml, programs tend to compile quickly and it's nice to know that you can get high performance at run-time without too much trouble. I also like having the imperative functionality available when it's the most natural approach.

Darren: I can assure you that scheme is NOT purely functional by any stretch. Also, as a dynamically-typed language it's not really in the same arena as Haskell and OCaml.

Aristotle Pagaltzis
2006-03-11 00:56:09
I have not used it myself, but have heard good things about Clean:


It’s on my list of things to look at.

Jim Apple
2006-03-11 17:50:38
I prefer Haskell - It seems to have a more active research community, which means new things are popping up all the time - template Haskell, Tim Sheard's Omega, Martin Sulzmann's Chameleon, GADT's, impredicative types, generic Haskell, Scrap Your Boilerplate, etc.

Haskell also has overloading, which is really hard to give up to go to OCaml. You can still do imperative programming in Haskell, it's just a little tricky. But you cannot do overloading in OCaml (without GCaml, which is non-standard).

2006-03-12 15:23:23
interesting, I guess you may find interesting AliceML.
It has a cleaner syntax than ocaml it seem (I find this strange since both are ml dialects in the end) and it has lots of interesting things such as builtin concurrency and constraint-based programming.
Jim Apple
2006-03-12 17:27:29
See the Haskell performance at http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=all
Jeremy O'Donoghue
2006-03-21 03:08:03

I have spent some time with both Ocaml and Haskell (as well as a little with Common Lisp), although my day job mostly requires embedded C code, which I've been working with for nearly 20 years.

Both are very fine languages, and you will learn a great deal from each. Ocaml has by far the better development environment, although some of the best bits only work on Unix systems. There's a decent set of debugging and profiling tools which are easy to use, and a proper (in Lisp terms) REPL. Whatever Haskell advocates (e.g. asdf) say, I find the Hugs/GHCi interactive environments fall far short of a real REPL environment because they do not facilitate the incremental style of development. Ocaml has this, and it's a joy to use.

Both languages are capable of very good performance, but it's easier to obtain in Ocaml - the Haskell website has pages on tuning Haskell for performance, and, frankly, some of what is required is rather ugly.

Both languages have an adequate set of libraries, with most things you would want being available, although with an inevitable bias towards computer science research topics (you may have more luck with theorem proving algoritmns than networking APIs for example). I like Haskell's FFI a lot better than Ocaml's, if interfacing to existing libraries is important, although with Ocaml you can use SWIG to do a a fair bit of the work.

I have a personal beef that practical GUI work on Ocaml is only really possible using GTK, which is revolting to install on Windows and Mac, and ugly on both (it's OK on Linux) - there is a (moribund) Wx bridge for Ocaml, but it doesn't work, at least on Windows (I gave up after three days pursuading it to build and a day or so trying to debug it). Haskell offers a very good interface to Wx, which looks native on all platforms (I use all of Mac, Windows and Linux, and prefer my work to execute on all of them).

I don't have a strong opinion on the syntax of either, although I would have to say that Haskell type classes definitely save a lot of repetitive typing compared to implementing similar functionality in Ocaml.

The Haskell community is a little larger than Ocaml's, although both are very helpful and friendly (as long as you're not asking for help with your homework). The Haskell list probably has the widest 'knowledge bandwith' of any mailing list I've ever seen, from total beginner questions to highly abstruse stuff which probably represents the cutting edge of PhD research. All are answered with detail and good humour.

So, overall, you have two very interesting languages which both have much to recommend them. Ocaml definitely has the better development environment, and they both have adequate (but could be better) library support.

From the foregoing, you migth assume that I'm mainly an Ocaml user, but in fact I'm finding myself increasingly using Haskell. Why?

Basically, it's because I want to understand new ways of thinking about software, and Haskell, by forcing me to think in a purely functional way, does a better job of this. I can't rely on the crutch of doing things 'the imperative way' and have to get to grips with new ways of thinking. I love the fact that it's stretching my brain in new ways. I still don't quite 'get' Monads, but I'm getting there. I'll also add a vote for The Haskell School of Expression which is one of the best CS books I've ever read - certainly on a par with SICP and On Lisp (two other favourites).

Whichever you choose, good luck - FP is a fascinating journey.

2006-03-26 09:36:10
I have used OCaml apart from C/C++/Java/Python but not Haskell.
In OCaml :
i) type inferencing is pretty good.

ii) static type checking is super strong but the type system is flexible to not come in the way most of the time. and this comes from a person who loves python.

iii) its fast. i write a lot of numeric code and python just doesnt cut it. i have tried using numarray for python for example but still too slow for my purposes. ocaml performs very well.

iv) many many times the following rule hold for ocaml programs which is just plain amazing : if it compiles it is correct.

v) i love python and enjoyed ocaml very much.

2006-04-18 13:33:56
You really should also consider erlang, it's IMHO the best functional language available.
Kimberley Burchett
2006-04-23 16:10:50
I prefer haskell to ocacml. Since you mentioned you had trouble doing dynamic programming for your bioinformatics class, I'd like to mention that I used haskell for all my assignments in my bioinformatics class, including things like sequence alignment, blast, suffix trees, shotgun assembly, and HMMs. Here's an example of the core part of the Viterbi algorithm, which is based on dynamic programming. It stores each column of intermediate results as an array, and subsequent columns are calculated via recursion. In brief, the core of dynamic programming in haskell is the use of arrays with scanl or foldl.

type State = Int
type Event = Int

viterbi :: [Event] -> ([State], Double)
viterbi events =
let initStep = mapAssocs (\(s,p)->([],p)) initProbs in
let steps = scanl viterbiOneStep initStep events in
let (bestPath, bestProb) = argmax snd $ elems $ last steps in
(reverse bestPath, bestProb)

viterbiOneStep ::
Array State ([State], Double) -> Event -> Array State ([State], Double)
viterbiOneStep prev e =
listArray (0,numStates-1) $
foreach [0..numStates-1] $ \s' ->
argmax snd $
foreach [0..numStates-1] $ \s ->
let (prevPath, prevProb) = prev!s in
prevProb + (transitionProbs!(s,s')) + emissionProbs!(s',e)))

In my class on NP-hard problems, I used haskell to solve most of the assignments (knapsack, traveling salesman, constraint propagation), and they usually ran as fast or faster than the C and Java implementations that others did. Ocaml implementations usually performed equally well. I used ocaml for a graph coloring assignment in that class.

In general, I found ocaml to be very straightforward, to the point of being almost boring. Compared to haskell, programming in ocaml almost feels like programming in C. I also found ocaml encouraged me to use state too often for my own good (I spent less time getting code to compile, but more time debugging). I prefer haskell largely for aesthetic reasons (referential transparency, strong emphasis on purity), so I guess it really depends on what you're looking for.

If you want a language to help you get everday work done, without having to think too hard about how to achieve what you want to do, then you should probably go with ocaml. If you're looking for a language to stretch your skills, and which encourages you to write in a style that scales better for really large programs, you might want to use haskell.

2006-04-27 21:03:38
I agree that OCaml's syntax is one of its weakest points. Hell, I even got here trying to google for "OCaml syntax".

let/let rec problem is not a compiler optimization. It comes from people wanting to do two things with a similar syntax:
let rec x = 0::x -- defines circular list
let x = 0::x -- defines list of 0::
By comparison, in SML function definitions (fun f x = ...) have let rec and value definitions (let x = ...) have let semantics, and it's impossible to define circular values.
So that's what you'd lose for getting rid of rec (unless you fixed the rest of the grammar at the same time).

Now, having coded a lot in OCaml and quite a bit in Python and Haskell (not much List though), I claim that the best language ever is Ruby :-) It got OO and functional programming integrated in a very natural way. Just look how mentally pleasing the code is:
$ ruby -e 'STDIN.map{|line| line.scan(/http:\/\/\S+/)}.flatten.each{|link| print link, "\n"}'
and imagine how ugly or verbose (or both) would it be, done in any other language.

If you're not sure if you should be interested, just take a look at some code at Ruby on Rails screencasts (http://www.rubyonrails.org/screencasts) or
Pleac-Ruby (http://pleac.sourceforge.net/pleac_ruby/ ).
The beauty and power of the code should speak for itself ^_^

Now, low level computing in Ruby can be pretty slow, however most of the time the expensive low level code executes outside of the program, like in a DBMS or some library written in C.

Enjoy :-)

2006-04-27 21:07:01
Of course I meant:

let rec x = 0::x -- defines circular list
let x = 0::x -- defines list of 0::(old contents of x)

I only used angle brackets, so it's not visible :-)

Richard Jones
2006-05-08 05:49:29
If you do decide to go further with OCaml, make sure you look at our collaboratively built OCaml tutorial (http://ocaml-tutorial.org/).

We use OCaml in an industrial setting, and we've released a lot of our tools as LGPL or GPL libraries and programs here: http://merjis.com/developers I'm particularly proud of our integration of PostgreSQL's SQL into the language, as we are now able to make SQL calls directly, without needing the tedious overhead of wrapper libraries and so on, and any type safety problems are caught at compile time.

As a language, it is a good one. Very concise and very very fast.


j h woodyatt
2006-05-17 22:57:34
This is my favorite language deathmatch of all time. I honestly don't know which side to cheer, though I've personally written a lot more OCaml code than Haskell code.

With Haskell-- pro: lazy evaluation + referential transparency + type classes + monad syntax sugar; con: composing monads can be tricky, state mutators are trickier, some pure functional data structures are weirdly inefficient.

With OCaml-- pro: eager evaluation + optional lazy + mutable variables + imperative/functional programming + object/class polymorphism; cons: composing monads is trickier, state mutation is always lurking somewhere, some imperative data structures will totally hose your functional code if you aren't careful.

Let us know which way you go and what you like and don't like about where you went.

2006-05-28 00:56:38
Hi Christopher,

I'm late to join in on the discussion but I'll start off with saying that both are great languages.
As someone else mentioned on here, learning OCaml is a good first step towards learning Haskell. Haskell has a steep learning curve but as a language it's remarkably elegant and clean and I would say programming is Haskell is more fun and more rewarding. However, in order to truly appreciate Haskell you really need to understand and become comfortable with the underlying concepts. This may sound obvious but it takes a while to get a good grasp of certain things in Haskell (such as monads).
It took me a few goes of trying Haskell before I 'got it'. And in the process, each time I went back to Caml and Scheme out of frustration. But in the end something finally clicked in my head and once I understood what monads are, how to use them, how to create them, everything that used to frustrate me in Haskell started to make sense.
From that point on the main struggle was squeezing enough performance out of GHC. It *IS* possible, but it is *NOT* trivial. GHC can be as fast as Caml but while in Caml naively written code will often still be reasonably efficient, in Haskell more often than not that won't be the case and you will have to give various hints to the compiler to get it to generate efficient code.
This may have come off worse than it really is - the main point is that there is a lot more to learning Haskell than learning Caml.

In the end OCaml is a good choice for the short term - it is far easier to learn and it is easier to guess how fast your code will run in the end. But in the long term I would say slowly switching to Haskell is a better idea. Once you get more comfortable with it you'll be able to write efficient code in Haskell and knowing Haskell will be beneficial to programming in other languages.

Haskell is one of the languages that can completely change the way you think about programming along with languages such as Lisp and Scheme, Forth, Joy, J, Smalltalk, Self, Erlang and Prolog.


Emre Sevinc
2006-06-03 01:51:44
What if you had Common Lisp with pattern matching, l calculus consistency, and optional static type checking?

"Qi programming uses sequent calculus notation to define types, and has the most powerful type system of any existing functional language, including ML and Haskell. Qi is available under the GPL licence and has been downloaded to over 200 institutions."

If you want to take a look at the Qi programming language:



It's fresh Usenet group:


2006-07-20 04:51:19
one possible answer for "getting more" could be Nemerle
2006-07-25 17:25:05
taw, two pieces of advice on your Ruby line:
1. Change /http:\/\/\S+/ to %r{http://\S+} (pick your favorite delimiters).
2. Change print line, "\n" to puts line.
3. Just make it this:
ruby -ne 'scan(%r{http://\S+}) {|link| puts link}'

(-n wraps your code in ARGF.each{|$_|...}.)

Sorry this is so off-topic; I'm in the first throes of learning Haskell, so I don't have anything to add. Just figured I'd clean up some Ruby code on the way. ;)

2006-08-04 03:06:04

I searched for "haskell vs python", that's how I ended up here.

2006-08-27 11:18:17
Change that to:

ruby -ne 'puts scan(%r{http://\S+})'

2006-09-29 10:40:29
If you want to do dynamic programming without dealing with monads, you could use a purely functional balanced binary tree.

2006-09-29 13:22:50
Another approach to dynamic programming:

Use an array comprehension. Be sure to import the Array module to do this.
This is an example of dynamic programming for the first 10 fibonacci numbers:

let fib = array (0, 10) ([(0, 1), (1, 1)] ++ [(i, fib!(i-2) + fib!(i-1)) | i <- [2..10]])

The idea is to place your dynamic programming algorithm in the comprehension itself.

a haskeller
2007-01-08 11:38:58
Coming from a purely imperative background i'll say that Haskell took more than a few days to wrap my head around. That said it was well worth it. Once you learn to think in Haskell you won't even wish for a variable to modify. Solving problems in haskell is more interesting than in imperative languages because there's no state management.
2007-01-19 19:50:26
I started off using O'CAML and probably would have stayed there if it weren't for the fact that support for windows machines is not what it could be, especially if you need to do a lot of FFI to C code. At that time SWIG didn't really work for O'Caml on windows, I'm not sure if that's still the case.

Now after having written a couple of large projects in Haskell, I am quite pleased with the results. If I need fast imperative code I drop down into C and use the wonderful FFI. Perhaps because of this performance hasn't been an issue, although since I do a lot of number crunching I always want things to be faster.

To really be productive in Haskell you have to understand monads. I recommend this tutorial.

Haskell syntax is considerably cleaner than O'Caml and the windows support is pretty good (probably because one of the main GHC contributors is employed by microsoft.) When writing real applications you'll find yourself using monads in about 60% of your code. Moreover type classes are overrated in my opinion and I did not have much of a need to use them. My code does have to keep fairly large state variables around, but most of the raw data matrices I keep in C arrays wrapped in a monad. Seems to work for me.

Now if you want raw speed and a syntax that is remarkably similar to Haskell, consider Clean.
They even have a 64 bit compiler that runs under windows! The drawbacks with clean are it's much smaller user base and library, and the fact that it is a bit tricky to export clean functions to C.

2007-02-06 22:43:23
Just for the record... the ';;' is not 'just for the interactive env.'

The ';;' is used to specify a 'chunk' of code that can be processed while ';' is used to seperate parts of that chunk.

To be clear: you only need to use ';' or ';;' if you have to. In other words - you won't see either in most code.

2007-02-06 22:44:45
Sigh... what I meant was - if OCaml knows what you meant then you don't have to tell it. If it can infer that the net token is a new chunk of code, then you don't need the ';;'.
2007-04-14 23:33:52
Interesting comments.. :D
2007-04-15 00:41:31
Interesting comments.. :D
2007-04-17 09:19:09
Hi Lucy! Photo I received! Thanks!
2007-04-25 03:14:50
Hi Sam! Photos i send on e-mail.
2007-04-25 05:27:25
Hi Sam! Photos i send on e-mail.
Jon Harrop
2007-06-26 02:19:35
We have been using OCaml commercially for several years but we noticed the increase in Haskell usage recently, probably due to the publication of several books. So we're evaluating Haskell (and F#) as well now. Sales of our own book (OCaml for Scientists) have quadrupled over the past year, so these languages are clearly catching on.

OCaml seems to hit a sweet spot in the expressive-fast curve that puts it in a league of its own. Thus far, I have yet to see any Haskell features that warrant switching languages but that isn't to say that the converse isn't also true.

2007-07-12 12:23:10
I came here by way of googling "haskell ocaml". For the past several months, I've actually been learning Ada, which is attractive to me for the strong typing and concurrency. However, it's not really a "hot" language these days, while I continually come across references to both haskell and ocaml lately. The idea of functional programming appeals to me. As I see it, however, the lack of support for concurrency could be a deal breaker for me going forward, especially since the applications I am interested (for example, image processing) could really benefit from parallel processing. Are there any plans for these languages to adapt some form of concurrency?
Loup Vaillant
2007-08-01 08:56:54
about taking advantage of multiple processors in Ocaml, you probably should take a look at OcamlP3l, here:

I did not really dive into it myself, but I think this is probably the only way (apart from explicitly forked processes), since for efficiency and simplicity reasons, the Ocaml garbage collector is unlikely to become concurrent (CF endless threads on the Ocaml mailing list).

2007-09-20 06:21:04
Christopher -
Since your Lisp studies are going so well, may I ask why you are looking into Haskell and OCaml? Does Lisp not fit your needs?
Christopher Roach
2007-10-11 11:09:19

In answer to your question, the main reason I wanted to learn either Haskell or OCaml is because one of the characteristics of Lisp that made it attractive to me in the first place was it's lean towards a more functional style and programming and the ML style languages, and Haskell in particular, seem to enforce that paradigm even more. Though at the end of the day I still find it easier to be more productive in Lisp than the other aforementioned languages, I still feel it is important to embrace new languages and programming paradigms to make yourself a more effective programmer.

2007-10-14 14:28:21
Thank you - I very much agree!

I found OCaml hard to wrap my head around, but Erlang and the tutorial that comes with the download made functional programming easy to understand.

If I can grasp functional concepts well enough, I hope to tackle F# for all the benefits of .NET framework programming. That will probably mean tackling OCaml as well, as it is modeled on OCaml.

2008-01-26 07:56:32
Definitely give Erlang a try, it's a lot of fun, at least, if not the language you will end up with.
Arthur Chan
2008-01-26 10:24:14
I really couldn't care less about the syntactical differences between the languages. I also couldn't care less about nitpicking a lot of the fine-grained details of the languages' features. As far as I've seen, Haskell's syntax does seem better, and Ocaml's seems almost kludgy at times (ever tried defining mutually recursive modules?), but this doesn't matter to me. I settled on Ocaml because of one serious overarching practical issue.

I wrote purely functional for a long time, partly as a way of relieving my frustrations with at-work code. As my applications got bigger, I realized that OO is actually better for certain things. As elegant as Haskell is, its insistence on functional style makes it difficult to do the sort of thing that academics don't do very much of: slave-coding. Every project has some slave coding in it, but it must be done. It's annoying to do certain high level user logic, especially on the GUI side, without OO.

Here is the kicker. Even after you figure out how to do it in the purely functional style, it'll still be well-nigh impossible recruiting other non-functional people into your project.

Paolo Ribeca
2008-05-07 09:42:42
Why are many people so much concerned about the syntax of languages? If you ever programmed FORTRAN, you will find that any other syntax is fairly trivial; and if you ever programmed any serious application, you will probably agree that what matters most is code compactness and performance. OCaml is definitely the best one as far as performance is concerned; and not only for this reason.
Personally, when I heard the first time about functional languages I started to learn OCaml and Haskell in parallel. I stopped to learn Haskell, however, when I first stumbled into the monad: as the last post correctly points out, there are things which simply you cannot do in a neat way if you do not have sequential and object-oriented features; and OCaml does provide them both.
I was able to learn the core of the language (excluding the OO-model) in about two weeks (the main constructs are three or four, and with a few idioms you are immediately able to use all the important data structures in the standard library like lists, arrays, sets, maps, and so on). OCaml syntax is difficult? Are you kidding? I remember a much longer time spent learning C, let alone C++.
Finally, OCaml is extremely fast, its performance being consistently close to that of C (even if sometimes you have to tune manually its garbage collector to get the best out of it, and this point is not very much emphasized in the literature I consulted --- some benchmarks are probably biased by this fact). And what I like best is its execution model, which is very simple and very well documented: in the end your program is converted to a list of simple operations on a very low-level data structure, with all the other higher-level layers, like for instance the type system, completely stripped off... crystal-clear, and amazing!
I really love OCaml. Chapeau to its architects!
2008-05-20 01:07:14
What about Scala? It's getting lot attention recently. Nice syntax, good static types system. Compiled to jvm bytecode - efficient almost as java. I'm starting to learn haskell to write better Scala programs.
2008-05-21 15:08:14
Great discussion here!

I have recently decided to dive into ocaml and not look back for at least a few months, hopefully longer. My requirements have boiled down to fairly specific demands, but also not too uncommon:

(1) an expressive static type system with modern functional capabilities
(2) compatibility with the important existing C/C++ libraries
(3) concurrent / distributed programming support (bonus: code mobility)
(4) a natural binding to Qt4 (failing that, a tolerable substitute)
(5) strong community with growth potential, academic and corporate use
(6) cross platform
(7) open source and not controlled by a distrustful organization
(8) possible to encourage my friends / colleagues use it
(9) IDE with code completion

Unfortunately, I don't actually know of a language that does all that, but ocaml is probably the closest. It has concurrency via join calculus (jocaml) but no code mobility. It has Gtk bindings but not Qt4 (although: http://qt-caml.crapulion.org/trac ). C/C++ library support is acceptable. It's cross platform, open source and headed by a sound organization (a france-based, computer science research institute). Rudimentary code completion exists via an eclipse plugin. Academia uses ocaml all over and at least one finance company has been successful with it. There is even an ocaml summer of code!

As for Haskell, I'm sure there are solutions to most of demands, but I don't know the answer to all of them yet myself. Hope this helps and good luck on your "perfect" language search!