elehack.net

A new favorite programming langauge?

For a long time, I’ve liked Python. My interest in it has dwindled some in the last few years, but I still return to it on a somewhat regular basis to get things done. Its relative cleanliness, well-defined and understandable semantics, and mix of paradigms is a pleasant environment.

I also enjoy Scheme and Common Lisp. In particular, Lisp’s macro system and the power of a good interactive environment such as Slime make these languages a joy to work with.

So what has come up when someone with these tastes finds a new potential favorite? None other than OCaml.

I started learning OCaml to play with the Ocsigen web framework. After some experimentation, I determined that Ocsigen’s memory usage was too high for my needs (256MiB is all I have configured on my web server VM).

But what I had seen in OCaml was appealing enough that I kept working with it and wrote my website compiler in it.

OCaml is a member of the ML family of languages, which feature static typing, type inference, and pattern matching in the context of a functional programming paradigm. OCaml adds support for object-oriented programming, providing a class and object mechanism.

Functional programming is a rather effective way to work; it’s easy to keep straight what’s going on in a program, and encourages development and reuse of many types of procedural abstractions. It encourages mixing and matching various pluggable tools (for example, different types of loops: map, fold, etc., and different operations to perform with them) to avoid unnecessary code duplication and provide idiomatic patterns for common operations. It also makes it easier for me to think about the state carried around in iterations, since iterations are usually expressed in recursive functions with each invocation forming a clear state barrier.

For a while, I’d been all about dynamic typing. Who wouldn’t love not having to mess with type declarations? However, dynamic typing doesn’t have the compiler making sure that you’re at least operating on the right types of data everywhere. Static typing with inference gets away from the messy declarations, and rather infers from operations performed what types of data are in use, and then requires the program to be consistent. It’s not a perfect system, and it needs some help in places (particularly recursive data structures), but in general, it works rather well. The rigorous type analysis has proved rather beneficial — I’ve found that, if my code compiles and thus makes it past the type checker, it usually behaves at least pretty close to correctly. It lets me focus on actual behavior problems, without having to worry about whether I actually have the right data types. And the pattern matching stuff is pretty nifty.

Finally, OCaml has its theoretical beauty coupled with decent interfaces to the messy real world. The Unix module provides access to a host of system calls, making interfacing OCaml with the outside world a breeze. This is a problem with some nice theoretical languages — Oz and many Common Lisp systems make playing with outside things rather complex and ugly.

But no language is perfect. OCaml has its warts. Notably its syntax — there are times when I’m pretty sure that I’ve found better syntax in my shoe. The nesting rules seem a little non-obvious and tricky to get right, and it takes a bit to figure out when you need a semicolon and when you don’t. But with some practice, it isn’t too bad.

There are probably other warts I haven’t found yet. From what we’ve seen thus far of Standard ML in CSci 5106, SML has some niceties that OCaml lacks (integrating pattern matching in top-level function definitions seems slicker in Standard ML). But when it comes down to a final verdict, I think OCaml is worth more of my time.

Comments

No comments posted.

Post a Comment

You may post a comment using the form below. All fields are optional. By submitting a comment, you release it to Michael and Jennifer Ekstrand under the Creative Commons Attribution 3.0 license. See our copyright notice for details. You might also want to read our privacy statement.