Go Back

Fun With Macros

This note is very much inspired by steve losh’s Fun With Macros blog posts.

Macros can be fun and sometimes helpful too, let’s play around with some macros I created in the last week or so!

Hash Table

By default, Common Lisp doesn’t provide literal hash tables, we can create a simple macro that let’s do something similar:

(defmacro dict (&body body)
  "Creates a hash table from a plist-like BODY.


  (dict :a 1
        :b 2
        :c 3)
  ; =>
    (SETF (GETHASH :A #:G239) 1)
    (SETF (GETHASH :B #:G239) 2)
    (SETF (GETHASH :C #:G239) 3)

  (let ((ht (gensym)))
    `(let ((,ht (make-hash-table :test 'equal)))
        ,@(loop :for (key value) :on body :by 'cddr
                `(setf (gethash ,key , ht) ,value))

We first create a new symbol with gensym, gensym guarantees that this symbol will not be used ever again throughout our program, this is pretty useful to avoid name clashes between symbols we define, as the macro’s author, and symbols the user defines and passes to our macro.

Right after that we assign a new hash table to the symbol we generated and loop through body. body should look something like this:

(:a 1 :b 2 :c 3 ...)

Then we will extract those key-value pairs from body to get (:a 1), (:b 2), and so on. And finally set key to value on the hash table.

Now we can use the following syntax to create new hash tables:

* (dict :a 1
        :b 2)

And since all we did was abstract the syntax, we can still use regular functions that act on hash tables with it:

* (gethash :name
           (dict :name "felipe"
                 :email "email@example.com"))

Extra Parenthesis Around let

When using let you probably noticed that each binding needs an extra pair of parenthesis:

(let ((x 10)
      (y 20))
  (* x y))

We would like to write something like this instead:

(let (x 10
      y 20)
  (* x y))

Let’s start by converting a property list to a nested one:

(defun nest-plist (plist &optional (acc '()))
  "Returns a nest version of PLIST.


  (nest-plist '(x 5 y 10))
  ; =>
  ((x 5) (y 10))

  (cond ((null plist) (nreverse acc))
        (t (nest-plist
             (cddr plist)
             (cons (list (car plist) (cadr plist))

This function will loop through our property list and create new lists containing the key and value, pretty similar to what we did in the hash table macro.

The macro itself is pretty simple:

(defmacro letn (bindings &body body)
  "Like let, letn creates new variable bindings but remove extra parenthesis around each binding.


  (letn (x 5
         y 10)
    (+ x y))

  `(let ,(nest-plist bindings)

Before passing bindings to let we first pass them to nest-plist, and then we just splice body inside let.


The magic thing about macros is that, in the end of the day, they’re just code. No matter how crazy and complicated one might be, they will become regular Lisp code you could’ve written yourself.

Like functions, that allow us to abstract funcionality, macros let us abstract syntax. This allows us to map our programs to the problem domain like no other tool.