Week 6: Clojure

September 23rd, 2013

Clojure is one of my favorite languages of the moment. Although I haven’t used it for real work yet, I enjoy the simplicity of the language and syntax. So with a week full of Clojure exercises I am in for a joyful week!

Clojure is a dialect of Lisp. It’s a functional language but not a pure functional one. One of the most interesting aspects of Lisp (in my opinion) is that the language is homoiconic. Simply stated this means that both code and data share the same internal representation. This makes the language ideal for metaprogramming.

Rich Hickey created Clojure because he wanted a functional, dynamic language for the industry dominant platforms of the JVM (Java’s virtual machine) and CLR (common language runtime, a virtual machine for the .NET platform).

Clojure is the first Lisp that is designed to be a tool for general-purpose programming. Its syntax is a little easier on the parentheses than other Lisps, so that less programmers are put off by them. Moving from foo(bar, baz) to (foo bar baz) should not be difficult for programmers.

Bruce chose Yoda as a character for Clojure. His communication style is often inverted and in the early beginning hard to understand. Especially if you’ve never read Lisps before, this is true for Clojure. Where the Jedi master has an inner power that others can’t seem to master, Lisp has macros and higher-order constructs that most no other language is offering.

Like I said I haven’t used Clojure for real work yet but my previous experience was enough to get me through the first days quick. On day 3 the sleeping barber exercise (by Edsger Dijkstra in 1965) was interesting and fun. I got to the average of ~475 haircuts in 10 seconds, whereas the theoretical maximum is 500.

My solutions to the exercises in the book are available in the 7-languages-in-7-weeks repository on my Github profile.

Clojure has a lot to offer. It is Lisp and therefore is extremely flexible and very powerful. Being on the JVM it can take advantage of the Java ecosystem and its libraries. Lazy evaluation features can simplify many problems by delaying execution until it is actually needed.

Clojure has an interesting approach to concurrency. STM (shared transactional memory) protects you from many race conditions by forcing state mutations to happen within appropriately protected functions. This really pays off for solving problems like the sleeping barber.

One of the greatest weaknesses of Clojure comes from being on the JVM. Optimization for tail recursion is limited to loop/recur. We can only hope that the JVM will fix this some day.

I like Clojure a lot. Once you get past the awkward feeling of the syntax it actually starts making sense. It is highly expressive and its functional nature allows for much more clear and beautiful abstractions that Java ever can. I believe that better software can be built faster with Clojure.

Next week is the last language of this book: Haskell. See you in a week.