Clojure Day Two

The second day of Clojure looks at lazy evaluation, macros, and writing classes to interfaces. Macros are a core concern of all lisps, so understanding this idea means I would be slightly less disoriented when reading something in another dialect like Common Lisp or Scheme.

  1. Things to find
  2. Some Clojure macros
  3. A lazy sequence
  4. Things to do
  5. The unless condition
  6. A type with defrecord
  7. Thoughts

Things to find

Some Clojure macros

Find the implementation of some of the commonly used macros in the Clojure language.

A whole bunch of core Clojure constructs are in fact macros. We can view the implementation of a macro by expanding it. Here are a couple of examples I found in the documentation:

A lazy sequence

Find an example of defining your own lazy sequence.

Clojure’s lazy evaluation is quite clever in that it allows you to define some infinite sequence, and rather than hang forever while it computes to infinity, you can just take the parts from the sequence that you need. I found a fairly contrived yet simple to understand example on Stack Overflow:

Things to do

The unless condition

Implement an unless with an else condition using macros.

With macros, we’re able to add our own control structures to Clojure. Defining a macro is similar to defining a function; the difference is that a normal function will evaluate all of its arguments, whereas with a macro we can delay execution.

Here’s my implementation of an unless control structure. I wouldn’t write this in production code, but it makes for a clear example.

An interesting extension of this would be to declare an optional third parameter that takes another body, so the user can choose what they’d like to do if the else branch of the conditional is reached.

A type with defrecord

Write a type using defrecord that implements a protocol.

Records and protocols are Clojure’s equivalent of Java’s classes and interfaces. Thinking in terms of classes and interfaces seems like a very OO-style thing to do, so I’m unsure where this fits into a functional language like Clojure. I think Rich Hickey spent four years lying in a hammock thinking about how to design Clojure. I think I’m going to need four years hacking with Clojure to understand it.

Thoughts

As I mentioned, Clojure is complicated and I think it’s not a language that can be skim-read easily. Given that it’s a functional language and it’s on the JVM, I’d say its a language that’s worth the initial time investment. I’ll make a mental note to write my Next Big Idea™ in Clojure.