Brief Introduction to Clojure

Clojure (pronounced “Closure”) is a relatively new programming language which runs on the Java Virtual Machine. This is roughly what it looks like:

defn say-hello-to [name]

 println “Hello,” name

Neat, huh? Well, ok, I was lying a little bit in order not to scare you, because… pss, Clojure is a Lisp!

You’re still here? Alright. I suppose I can show you the real program, which is what I just showed with a few parenthesis added, in fact not that many:

(defn say-hello-to [name]

 (println “Hello,” name))

Being a LISP has its advantages and disadvantages. A disadvantage is that people get confused by the parentheses, or at least by their placement, because, let’s face it, in a C-style language, this definition would look as follows:

int main(char* name) {

 printf(“Hello, %s”, name);

}

Now, if you’re willing to extend your definition of parenthesis to include other types of brackets and braces, you will see that the number is actually the same. But the truth is, parentheses in usual places scare people, it turns out.

A nice, but potentially confusing feature of LISPy languages is that they are homomoronic… I mean, homoiconic, which Wikipedia says, means that the primary representation of program code is the same type of list structure that is also used for the main data structures. Great. What does that really mean?

Let’s see what happens when you type in a Clojure expression, for instance the following expression to add two numbers:

(+ 2 1)

The first thing that is invoked is the reader, which parses the expression and turns it into, in this case, a list containing 3 items: +, 2 and 1. Because we asked Clojure to evaluate the expression for us, this list is going to be evaluated by first evaluating all of its elements. It turns out that ‘+’ in in fact a defined function, so we get back a function object, 2 evaluates to itself as does 1. The evaluation of a list in a Lisp means to call its first element as a function with the rest of the elements as arguments. So in this case the + function will be invoked with 2 and 1 as arguments, resulting, not surprisingly, in 3. So lists are Clojure’s representation of function calls, except that the function name is put inside the parenthesis rather than before it.

Note that this makes Clojure, and Lisps in general, a language with an extremely simple and concise syntax. All there are are literals (such as characters, strings, symbols and numbers) and lists (although Clojure also adds special syntax for sets, maps and vectors to that). There are no special operators or keywords.

Fantastic. So, in a LISP all function calls (including operator calls) are written in prefix notation, i.e. instead of writing 2 + 1 you write (+ 2 1). You can also compose them, e.g. (+ 1 (* 2 3)) results in 7. Although this may seem a little confusing to read, its “regular” infix counterpart is 1 + 2 * 3, where you always have to take the language’s precedence rules into account, is it (1 + 2) * 3, or 1 + (2 * 3)? Using the Lisp notation it’s always clear. Now let’s look at the following expression:

‘(+ 2 1)

Note the quote there. What does that do? Well it quotes the expression, telling the interpreter: “do not interpret this, but return it literally”. So this expression results not in the value 3, but with a list containing three items: the + symbol, 2 and 1. We can now manipulate this list as we wish:

(second ‘(+ 2 1))

results in the value 2, because the second function returns the second item of the list that is passed to it. What we can also do is eval it:

(eval ‘(+ 2 1))

What eval does is *drumroll* evaluate the data structure that is passed to it as if it were an expression, resulting in… 3!

We can also manipulate lists a bit, so let’s say, get rid of the + and replace it by -:

(cons ‘- (rest ‘(+ 2 1)))

The cons function builds a new list with its first arguments as the head and the second argument as its tail — the rest of the list. The quote before — means “don’t resolve it, just give me the literal symbol -”, the rest function returns the tail of the list that is passed to it (so all items except the first one). The result of this expression therefore is:

‘(- 2 1)

We can then eval this expression, resulting in 1:

(eval (cons ‘- (rest ‘(+ 2 1))))

Now what you will have noticed, as mentioned, lists and programs are represented using the same syntax in Lisp languages: lists. Therefore, meta-programming is very natural and easy. Meta-programs are programs that manipulate (other) programs. And who doesn’t want to write meta programs?

I do. It’s my job. Plus, it’s awesome.

Lisp originally came out of the artificial intelligence world, where they had the belief that a homoiconic language at some point could transform and improve itself. Programs that rewrite themselves and at some point could make themselves smarter. Although it’s a cool idea, it never really happened. Shocker.

However, a great feature of Lisps resulting from their homoiconic nature are its language extension features. Have you ever wished that your favorite programming language had feature X? That you could use LINQ-style queries in Java, or PHP, for instance? The way to “extend” your language with features like this is to develop APIs that somewhat look like queries, but this will only get you so far. For instance, here’s an example of LIQUidFORM in Java:

Person p = LiquidForm.use(Person.class, “p”);

List people = em.createQuery(

 select(p).from(Person.class).as(p).where(eq(p.getSurname(), “Smith”)).toString())

 .getResultList();

It’s nicer than simply typing in “SELECT p.* FROM Person AS p WHERE p.surname = “Smith” as a string, I guess, but it’s kind of a hack. Here’s the same query written using ClojureQL, a similar framework for Clojure:

(def people-query (query * employees (= surname “Smith”)))

Now although you may argue it’s not pretty (because you find Lisp syntax ugly), but it clearly fits in well with the rest of the language and it’s way less verbose.

Similarly, when playing with program transformation in Clojure, I needed a pattern matching feature, which is only offered in a very limited form in Clojure (data deconstruction). So I built pattern-rewrite macro that enables me to define pattern matching as follows, quite similar to Stratego. I gave identifiers starting with “?” a special meaning, those are the variables that are matched in the pattern. The syntax of the patter-rewrite macro:

(pattern-rewrite expr

 (lhs-pattern rhs-pattern)+

 no-match-result?)

So, let’s rewrite our replace-addition with substraction example using this macro:

(pattern-rewrite ‘(+ 2 3)

 (+ ?x ?y) (- ?x ?y))

Resulting in:

‘(- 2 3)

Of course it also works with more complex patterns.

The idea of being able to add features like this to a language really appeals to me. You can do stuff like this in other modernlanguages, like Scala and Ruby, in a nice way as well, but Lisps still offer more power due to their simple syntax.

You can learn more about Clojure on their website. Pragmatic Programmers have a nice Clojure book: “Programming Clojure”.