Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 8 down vote accepted

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.

share|improve this answer
I have just bought the book "Practical Common Lisp", I will have a look at the example you have suggested. – Giorgio Dec 28 '12 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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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