Depressingly predictable
comments on the Haskell story in Slashdot yesterday such as:
"I have *never* seen it being used since. To my mind they both belong in the category 'interesting, but pointless'."
and
"The point is that there's nothing those languages can do that can't be done, often more easily, with the current crop of popular languages. Elegance cannot beat convenience in the workplace, or in most at any rate."
and so on.
Here are some useful things you can do in OCaml which you cannot do in the "current crop of popular languages".
1. Change the language to suit your task.
In our case the task was to parse binary formats.
If you have a few hours to spare, try writing a C program to parse tcpdump files. Don't forget that the endianness in these files is variable so you'll need lots of
if (..) { field1 = htonl (field1); ... }. OK so that's a bit hard. Let's say you want to parse a 6 bit length field 'n' followed by an
n+1 bit data field (as a 1-64 bit int). Go and write it in C now.
Using our
bitstring extension to OCaml, parsing binary structures is really effortless:
let bits = Bitstring.bitstring_of_file "input.data" in
bitmatch bits with
| { n : 6;
data : n+1 } -> data
This is the complete tcpdump parser, just 113 lines of code.
And it's fast too. The resulting code compiles down to machine code and inlines
direct calls to C functions at every opportunity, so in practice you can parse data at speeds approaching C.
Another task we had was to check that hundreds of SQL statements in a modest sized web application actually matched with fields in the database, in other words that they didn't reference non-existent fields or treat a string field as an integer and so forth. Doing this by testing is almost impossible because many SQL statements are only used on rare error paths. We could contemplate doing it manually
once but not routinely.
So instead we extended the OCaml language to do the checking for us
every time we compiled the code. The resulting
PG'OCaml project gives you compiled-time checked type-safe access to your database. It's used by mod_caml and ocsigen, or you can just use it in standalone programs. I won't go into detail because
Dario Teixeira wrote a great introduction and tutorial to PG'OCaml.
Martin Jambon, author of Micmatch which adds regular expressions directly into the language, has an
excellent tutorial. Browse the
list of syntax extensions here.
2. Get the compiler to check for errors in your logic
Why can't your compiler check for errors like when you use a read-only database connection in a SQL INSERT statement, or you are supposed to call library function
set_word_size because you try to call
get_word?
In OCaml, using
phantom types [tutorial] you can do exactly this.
3. Drop down to imperative code when you need speed like C
Scripting languages are expressive but slow ... and getting slower. But there's no need to compromise expressiveness to get real speed. Static typed languages with type inference (which excludes C, C++, Java, C# and most others, but includes SML, OCaml and F#) give you the expressiveness to write compact code but
without compromising on speed.
OCaml programs are more than
10 - 15 times faster and less memory intensive than Python programs.
More about speed and optimizing OCaml programs. Comparison of a
raytracer written in C++ and OCaml.
4. Large well-established standard libraries
OCaml comes with
hundreds of packages for many tasks now.
If that's not enough you can directly call out to
Perl,
Java, Python and
.Net [on Windows] libraries. Or you can call C functions directly.
You can
compile the same code on Unix, Mac OS X and Windows.
So enough of this "elegance can't beat convenience" stuff please.