Clojure: a stateless dynamically-typed Lisp on the JVM

I’m a big fan of programming without state in languages like Haskell and Erlang. Clojure is a modern Lisp, but follows the stateless style very closely, taking ideas from Haskell and ML. Clojure is implemented in Java and runs on the JVM, providing full access to all of Java’s libraries.

Modern

Clojure is a lot less crufty than other Lisp implementations, some of which are now pretty long in the tooth. Maps and vectors and a few other things are given syntax. Macros still work though, so I guess it’s still all s-expressions ([1 2 3] could trivially map to (vector 1 2 3)). Clojure has a terse syntax with lots of good code smells (e.g. *constant* and predicate?).

Structural sharing

Clojure benefits from state of the art data structure research. Clojure’s key data structures (lists, vectors, maps) are implemented using a technique called structural sharing. If you add an element to the front of an existing list, the new list is simply the new item plus the existing list. The existing list can be stored once. The new item simply points to the head of the existing list. This technique preserves complexity characteristics of operations on data structures (insert, remove, access, etc), minimises memory footprint and provides immutable state! Very cool stuff.

Dynamic typing

Clojure is dynamically-typed like Python or Ruby. This makes it a good entry point from these languages. For programmers looking to program without state other options are Haskell or Erlang. Erlang is awesome for building highly available, concurrent applications. It’s syntax is kind of fun, but not for everyone. Haskell is strongly typed, pure and lazy. This unique mix of features can be very powerful, but can also trip you up. Both languages have a sweet spot, Clojure feels more general purpose. For me, that means great for scripting and writing web apps :-).

I like Django’s minimal template language. There is an implementation in Erlang, but Erlang’s string processing is somewhat weak. Templating and other string processing, like parsing JSON, is a bit long-winded. Haskell doesn’t have any good template languages, the best option is Text.Xhtml, a combinator library. Not very designer-friendly. Haskell’s powerful type system isn’t very useful for implementing a text template language. It gets in the way more than it helps. It feels like an impedance mismatch. It would be an interesting project to build a template processor compatible with Django in Clojure.

Recommended viewing

For a more complete description watch Clojure creator Rich Hickey describing Clojure and walking through a concurrent application.