I am trying to learn some LISP and I have read a lot about the importance of LISP macros so I would like to get some working experience with them.

Can you suggest a practical application area that would allow me to use macros to solve a real-world problem, and to understand the usefulness of this programming construct?


This is not a generic what project should I do next question. I am interested to understand which kinds of problems are typically solved by means of LISP macros. E.g., are they good for implementing abstract data types? Why was this construct added to the language? What kinds of problems does it solve that cannot be solved by means of simple functions?


2 Answers 2


Lisp macros combine a few distinct properties:

  1. Macros define new syntax ↔ they use source-code as input
  2. Macros usually are run at compile-time
  3. Macros generate source-code

The best applications make use of all of those aspects. Probably the most-well known example is the (loop …) in Common Lisp, it wouldn't be anywhere close to its usefulness without one of those features. Without the source as input it would be awkward to define the actions inside the loop; without compile-time expansion it would be way too slow; and without code-generation it wouldn't be executable.

Another fine example is the binary serialization chapter in Practical Common Lisp, Practical: Parsing Binary Files.

A query macro that implements something similar to LINQ might be another good application. But it would be missing the auto-completion that makes LINQ as nice as it is. Almost anything that today is solved by special purpose code-generators with XML inputs (e.g. XAML) could also be implemented using Lisp macros.

  • I have just bought the book "Practical Common Lisp", I will have a look at the example you have suggested.
    – Giorgio
    Commented Dec 28, 2012 at 1:29

The way I like to think about macros in common lisp is that they are functions that return code to be evaluated, but 'do not' evaluate their arguments before returning that code. Functions return something too, but they evaluate each argument before their body is evaluated. Macros do not.

Paul Graham in 'On Lisp' provides (IMHO) one of the best descriptions of the differences between macros and functions, and discusses their overlap and uniqueness. Many bits of code can be written as either a function or a macro, but there are cases where only a macro will work. Once you wrap your head around this, I think you've got the gist of macros in lisp.

As an example of a macro where a function will not work, check out 'aif' (anaphoric if). It's just a few lines of code, and my recommendation on where to start.

And do reference Common Lisp's version of anaphoric if, that takes advantage of intentional variable capture. Scheme's automatically-hygienic version is not IMO the 'real deal'. Variable capture is an important part of lisp macrology, and some of the more powerful and useful macros certainly take advantage of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.