# Seven Languages in Seven Weeks Clojure Day 2

Today Clojure’s coverage is getting beefier with tail recursion (soso), lazy sequences and … macros!

The support for tail recursion is disappointing. But given that the JVM implementers never actually understood the value of tail recursion (or perhaps they overestimated its cost), it would have been surprising if a language running on top of the JVM had a solution. Scala is similarly limited.

Lazy sequences are cool, but not spectacularly so. It is a more restricted concept than laziness in Haskell. Still, lazy lists form a large part of my use of Haskell’s laziness (perhaps the only part I can use), so I do not expect to feel limited in any way with Clojure.

Finally, macros. Writing macros separates men from boys. Writing macros that write macros separates gods from men. Macros are good. Abuse of macros can cause headaches, but the good kind (it’s just your brain swelling).

## Exercises

### Implementation of common macros in Clojure

That one is very easy. The web site has a dedicated page with the common macros. The source code is available for each. `defmacro` is actually a macro, and its source code is, well, let’s just say I’m happy someone else wrote it. And yes, it is a macro that writes macros.

To expand the code of macros, I wrote this small function to recursively expand macro definitions:

Without it, it is difficult to see deeply into the code:

`macroexpand` did not expand the `cond` macros.

With `rec-expand`:

### Implementation of a lazy sequence

For this exercise, I use `lazy-seq`, which evaluates its body on demand (and remembers the value).

One first example is a simple reimplementation of `(iterate inc n)`:

Testing it:

A bit more interesting, perhaps, is a Fibonacci sequence not using `map`:

Computing the 20 first Fibonacci numbers:

### Unless with else condition

For this exercise, I use two features that were not covered in the book

• variable lists of arguments support
• backquote notation

Clojure support for variable lists of arguments is nice: there can be a different body for each list, and the last one can a placeholder variable for “all the remaining arguments”. The concept of arguments (and variable lists) applies to everything that takes arguments: functions, macros, … Here I use it to differentiate between basic `(unless test body)` and `(unless test body else)`.

Backquote notation is what makes macro useable. Rather than using `list` to build the form, I use the backquote. Then expressions prefixed with tilde are replaced by their value. Lisps had an identical concept (but comma was used instead of tilde).

This makes macros shorter and easier to read.

Testing it:

### Using defrecord and defprotocol

Ok, I’m not very inspired by this exercise. I was thinking of doing the classic shape class hierarchy, but finally settled for an employee protocol:

The first implementation is a manager, which is modeled according to things I’ve heard about other companies, not mine, where managers are hard-working and have to be pulled from their desk to be fed:

Testing the manager:

Of course we need people to actually do something:

Testing the worker:

Ok, nothing fancy. But looking at the doc I see it is possible to extend basic Java classes, so I think there’s far more depth to this construct. Otherwise, there’s always `defmacro` to play with.

And this completes Day 2.