The Joy of Clojure

September 9, 2012 at 8:24 pm
filed under Coding
Tagged , , ,

I just finished reading The Joy of Clojure. Overall I would recommend it as a good way of getting an introduction not just to Clojure, but Clojure philosophy.

As a language, I’m not sure anything I have to say about it so far isn’t anything that would shake out of a comparison between a pure, statically-typed language like Haskell. Still, there are some properties which make Clojure unique enough and have stuck out in my mind.

Chiefly, the presence of exceptions (and thence try/catch) and nil are two stumbling blocks for me. In the book, I would occasionally see examples of exception throwing or handling, and frankly I would cringe a bit. I suspect that in part this is a manifestation of pragmatism; if nothing else, Java interop necessitates some way to deal with exceptions.

Without going too much into it, I’m not a huge fan of exceptions and what they do to control flow. Josh Bloch’s talk about How To Design an API and Why It Matters cautions against the use of exceptions as control flow, and against checked exceptions. Clojure seems pretty consistent with that when you think about it, so it’s possible I’m overreacting.

Ironically, of course, I wish there were a way to turn down the purity of Haskell a bit, at least here and there. The beauty of Haskell’s type system is that it is so safe, and that it’s extremely flexible. But like it or not there is often friction, such as where you have to glue together two types which might be incompatible.

As I understand it, Lisps in general are not strongly typed, and seem to favor only a very small number of relatively simple types. Clojure is consistent with that. You can build your own types, but generally you rely on composition of much smaller, simpler building blocks: functions, maps, vectors, and sets. Which brings me full circle, back to the above, about Lisps in general.

What I’m finding is that Haskell’s emphasis on purity and safety has in some ways retrained my brain to be wary of proliferation of implicit such that I probably would not have found Clojure nearly so disturbing in some ways had I studied them in the reverse order. There’s stuff I miss from Haskell, like how easily you can compose functions; the power of pattern matching; currying; the crisp, clean syntax; and so on.

On the other hand, I think of Clojure how I used to think about Python, as something very powerful and relatively simple. It’s that simplicity in particular that appeals to me. Also, the differences between Common Lisp and Scheme versus Clojure are apparent from reading SICP and On Lisp, but there’s far more similarity than difference. That’s a huge advantage; people have been writing about Lisp for decades. Haskell is a lovely language but it is much closer to niche, and I would argue that it’s almost in its own world in terms of its very intimate relationship to high mathematics. Lisp seems downright earthy by comparison.

What is next

In the last year or so, I’ve now spent time with three languages which were new to me: Go, Haskell, and Clojure. I almost feel like these form a sort of holy trinity of modern programming with elegant sensibilities. The challenge is either to keep learning any of these in depth, sticking with them in effect, or whether I branch out even further and start looking at languages like Erlang. Alternatively, I hear Scala has potential.

At least for the time being, I am interested in getting as comfortable with Clojure as I am with Haskell. I anticipate this being a lot easier; as I said above, Haskell is wonderful but rarified. Clojure takes pains to ephasize purity but when it comes down to it, it reads and writes much more like a, er, traditional functional programming language than one which is as pure as Haskell.

%d bloggers like this: