Friday, September 14, 2007

OCaml, my love

I want to love OCaml, I really do. In fact, I want to love Haskell too, but OCaml seems just a little more pragmatic.

But in the end, there is something that keeps me from loving OCaml: I can't find a compelling use case. I can't see anything that's significantly easier to do in OCaml than in Lisp, for example. And while OCaml apparently make very fast executables, Lisp is certainly "fast enough."

OCaml doesn't seem to own anything I want to learn. Lisp owns AI, Erlang owns concurrency. I know, there are other options in both categories, but you get the point.

The most interesting feature I can see in OCaml from where I sit is the type system. No question, OCaml's typing is strong and static. From an outsider's point of view, it seems much like Haskell's; although I am sure devotees of either tongue would recoil in shock from such a suggestion. But the rigidity of the type system is a barrier to learning the language. You'd think there would be some sort of evangelism based on that; instead the OCaml-istas seem more interested in looking down on others for not having an adequate type system than giving examples of how such a system is a Good Thing.

I bought Practical OCaml, and I read it. More than once. But it fails to present me with interesting solutions to problems. Online tutorials have a similar problem "Look! You can add two numbers in OCaml!" Hardly worth learning another whole language, especially one with very different syntax from the ones I already know.

And when the language has a bondage-and-discipline type system, even adding two numbers seems inordinately difficult. I mean, in OCaml there is no auto-boxing that I can see... none! Even the arithmetic operators are different! "+." adds floats, "+" adds integers.

I suppose there is an opportunity here for me to learn OCaml and bring a decent book on it to market. Maybe I'm looking at this the wrong way.

So if OCaml isn't compelling, then why do I want to love it? I suppose because there are some interesting features in functional languages (like Haskell, OCaml, and Erlang) that intrigue me. One is the whole "clause" thing. Where a function in C might look something like this:

int add(int i, int j){
return i + j ;
}


And in Java it might look like:

public int add(int i, int j){
return i + j ;
}


That same function in OCaml looks like:

let add i j = i + j;;


But OCaml gets more interesting when you differentiate between inputs. So you could write some function that returns the sum of a list of numbers like:

let rec sum list = match list with
[] -> 0;
| head :: tail -> head + sum tail ;;


Actually, that's pretty cool: there are actually two definitions ("clauses") in the function. One is the definition of the sum of an empty list '[]'. That sum is zero. The other clause says if you get a list that has some data in it (i.e. a head and a tail), the function adds the head to the sum of the tail.

I suppose Lisp could do the same thing with DEFGENERIC, but I find the simpler recursive solution more intuitive:

(defun sum (LIST)
(if (null LIST)
0
(+ (first LIST) (sum (rest LIST)))))


So I suppose the OCaml version is shorter than the Lisp version.

Anyhow, I'm just rambling now. Programming is only interesting when you're learning something new. Right at the moment, my Java job is getting a little mundane. I had hoped OCaml would provide an interesting diversion, but it appears I was barking up the wrong tree.

2 comments:

freedomnan said...

I only know English; the odd word of French or Spanish. Even though so many people contend that English is cumbersome, bulky and awkward, I find it very usable, even poetic. I also am able to convey abstract thought and avoid profanity, though I realize that is a function of vocabulary.

clumsy ox said...

Well, English is an increasingly rare skill set. Or so my informal observations indicate.