Lisp Macros – Typical Applications in Functional Programming

functional programminglispmacros

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?

NOTE

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?

Best Answer

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.