November 29, 2005 2:02 PM   Subscribe

objective caml - is it worth learning?

I am considering learning ocaml as a good general purpose language that seems to do everything (functional, imperative and procedural) efficiently. Most of the programming I do is scientific, which involves massaging data, crunching it and generating pretty output (reports, figures). I am very curious to hear about your experiences with it. What is it actually good for and what not.
posted by blueyellow to Computers & Internet (9 answers total)
It's a fantastic general purpose language. I used to use C++ for my scientific simulation code, and have totally switched over to OCaml. Typically I achieve about 80% of the speed of C++, but I am 3-4 times more productive in OCaml--it's far more expressive, and I find that my code usually runs correctly by the time I get it past the exceedingly strict compiler.

On the downside, OCaml has relatively few libraries available (look here); however, writing bindings to C code is not difficult. Generating pretty output would not be one of its strengths, so you might end up producing a plaintext data dump as output and formatting/displaying it with an external program.

I might drop a link to mlGrace, a package I wrote for creating simple 2D plots using a nice API.
posted by Galvatron at 2:47 PM on November 29, 2005

Googling your question leads to this interesting post.
I had no idea that language wars were so strategized.
posted by metaculpa at 5:05 PM on November 29, 2005

there's nothing much wrong with ocaml, and it's probably the most used of the functional languages (perhaps because you don't have to use it as a functional language). in a more perfect world we'd all be using it instead of java.

as galvatron says, the lack of libraries is the biggest drawback (and why i'm currently writing a project in java rather than ocaml).

one other piece of advice - since ocaml tries to give you "the best of both worlds" you can do both oo and functional programming in it. unfortunately, if you try to do both at the same time, and you're still learning (like me), things can get a bit muddled. i tend to use the language without the object-oriented bits (structuring things with modules instead). if you're coming from a c++ or java background, you may prefer to stick with the objects and ignore modules at first.

what is it good for? it's a general purpose programming language. like java or c++, but with better typing and more support for other programming styles.

you should make sure that there's library support for the pretty output you want before starting, though (see comment about libraries).

but in answer to the bsaic question - should you use ocaml? hell, yes!
posted by andrew cooke at 5:25 PM on November 29, 2005

metaculpa's post is interesting, and pragmatic/realistic, but a bit misleading in that ocaml is very different to python. both are modern, elegant languages, but ocaml is statically typed with inference. that means the code looks like python (no type declarations) but is checked by the compiler (like, say, java), giving you less run-time errors and reducing the need to test.

it sounds crazy, but it's quite normal for the code to work correctly once it compiles.
posted by andrew cooke at 5:29 PM on November 29, 2005

I'm a PHP developer and I've recently been struggling with the urge to pick up another language, just for the heck of it. I've been leaning towards Python, if only because several open source projects I admire (The Gimp, OpenOffice.org) provide Python interfaces, so that at least seems to lend credibility to it as a language (which is not to say Python doesn't have credibility all of its own, but it's hard not to look for guiding clues when there are many, many languages to choose from). In doing some recent reading, however, I've run into the claim that Python isn't well-prepared for web development (see - the post is undated, but quotes PHP usage up to Jan 2005, so I assume it's recent)? So, I'm curious - is ocaml a reasonable web development language?
posted by planetthoughtful at 8:18 PM on November 29, 2005

python has zope/plone. it's got quite a steep learning curve, but i've used it and was quite impressed.

i don't know of anything similar for ocaml.

i'm working on a web/database thing at the moment and switched to java (which i don't like as much as ocaml or python) largely because it has so much more support for that kind of application.

but i'm not an expert. you should check out their web pages and, if you don't find the answer, use their support groups.

blueyellow - have you seen this book? i just stumbled across it now; seems like it might be useful.
posted by andrew cooke at 8:33 PM on November 29, 2005

Response by poster: Andrew: Thanks for the pointer. I did notice the book online, and examined the free chapter. The price is kind of steep at 85 pounds though. The free online source code might be interesting and a good way to get a handle on how to do some numerical things in ocaml. I am encouraged that both you and galvatron use it for cpu intensive tasks with good results.
posted by blueyellow at 9:22 PM on November 29, 2005

If you're concerned with squeezing out the best performance you can, I've got a couple of hints:
  • Avoid using the object system; it introduces some extra overhead, and method calls are more expensive than ordinary function calls. (Anyway, it seems like very few OCaml programmers make much use of objects... they're not so compelling when you have a good module system and first-class functions to rely on.)
  • Imperative programming is your friend. Programming in a functional style tends to involve a lot of manipulation of immutable variables, which means a lot of memory allocation operations. If you're not careful, you can spend half your CPU cycles doing garbage collection. You are better off using mutable data structures (arrays, mutable records, references) and reassigning their values as necessary. If you look at the optimized OCaml code posted at the Computer Language Shootout, you'll see that much of it is written in an imperative style for reasons like this.
  • Read Xavier Leroy's notes on writing efficient numerical code. (Better make a copy of that, as it seems to have vanished after the recent caml website update.)

posted by Galvatron at 10:12 PM on November 29, 2005

I've run into the claim that Python isn't well-prepared for web development

A ludicrous claim. It exists only because Ruby on Rails has received endless geek attention as of late.

The fact is that Python comes with a bunch of web service libraries and has about a dozen serious web-development frameworks, most of which have been continuously developed for several years now. Regardless what you wish to do, Python has the tools for it.

True, most of them aren't as slick as Rails... but, then, they're not trying to provide what Rails provides. Several of them are as slick as Rails, but in a different manner.

Check out Zope, Django, and Twisted Matrix. And check PythonWiki for more.
posted by five fresh fish at 10:34 PM on November 29, 2005

« Older For only 29 cents a day, you can help a geek get a...   |   I am a golden god! Newer »
This thread is closed to new comments.