OCaml and me

I have a few recurring test projects I do with new languages and environments. One of them is to put together the rudiments of a simulation featuring characters who get AI ticks, have characteristics and statuses, can be described in text, etc. Usually this gets far enough that I can task one character to chase another one around in circles, and then it's time to move on.

This weekend I did the first few strokes of this project in OCaml, a language that's interested me for a decade or so. In the last few years, the quality of documentation has improved a lot (check out the sweet website), it's picked up some industry support (notably Jane Street), and a nascent English-speaking community (OCaml's principal development has always been in French) resulting from the recently enlarged profile of statically-typed functional languages.

OCaml resembles Haskell in a number of ways. It's a direct descendant of ML, which Haskell was heavily influenced by. Both languages use a form of Hindley-Milner type inference, and the syntax generally feels similar.

Unlike Haskell, though, OCaml is strict (versus Haskell's laziness) and impure (functions can have side effects and contain imperative code; no IO monad necessary). It permits mutable variables, arrays, strings, and maps, all explicitly declared. It even has a neat object-oriented programming facility built in as a first-class participant in the type system and which works seamlessly with functional code. It's explicitly a multi-paradigmatic language, which appeals to me a great deal. The community still strongly encourages writing pure functional code, but in general I've found them to be less fanatical on the topic than Haskellers. There's also less of a math/PLT focus in the community, and idiomatic OCaml is less terse and symbolic than Haskell.

What I worked up is here. Once I've spent a bit more time fleshing it out, I'll write more about it. For this project, I'm trying out Batteries, which is one of several competing alternative standard libraries for OCaml. (What it means to have alternative standard libraries, and why such a thing has happened, is a subject for yet another post to come.) On other projects, and on the OCaml exercises at Exercism, I've been using Core from Jane Street. Core is also what's covered by the book Real World OCaml, which has been a fantastic resource.

Learning OCaml has been an exciting process so far. Ruby is very dear to me, and I've done a lot of work I'm proud of with it, but I think it's good for a programmer to stay flexible on the tools they use. OCaml provides a number of strong contrasts to Ruby (compiled, functional, statically typed) while retaining elements that I appreciate (object oriented features, a multiparadigmatic approach). I can imagine using it for future projects where the type system would come in handy, where I need high performance, or where I'm looking to deliver a compiled binary. And sometimes it's just fun.