lispreader-macro

Read macros: what do you use them for?


I'm trying to get a feel for the parts of Lisp that I haven't used very much up to now. Read macros have caught my attention at the moment. There isn't a huge amount of info about their use and it would help to see what people have done with them, both to get examples of how they work and also to see what sorts of problems can be approached with them. Following on that, are there any guidelines for knowing what constitutes good and bad use of read macros?


Solution

  • S-expressions are Lisp's syntax for Lisp data. S-expressions are read with the function READ and read macros are Lisp's built-in way to extend the reader. This means that the most direct use of read macros is to implement the pre-defined data syntax and open up possibilities to change or extend the way Lisp reads s-expressions.

    Lisp comes with a pre-defined external syntax for a lot of data types: symbols, numbers, strings, arrays, characters, conses, lists, structures and more. It allows data objects to be printed and read back.

    1. Lisp lacks syntax for several other data types - prominently hash tables and CLOS objects. So the first use of read macros in user code would be to extend the reader to be able to read data structures like hash tables, parallel vectors, new number types, ... basically every data type the developer wants to have an external syntax that can be read back.

    2. Since Lisp uses s-expressions also for code, the second use of read macros is to extend the notation for Lisp programs. A typical example is the use of [ and ] to write embedded SQL code. The usual Lisp syntax looks similar, but the use of [ and] helps the SQL expressions to stand out in the code. Another example is to use read macros to provide identifiers for embedded programming languages, like Objective C constants, messages, etc.. Clozure CL uses this to represent case sensitive / case preserving identifiers and to look up their definition during read time using an index of externally available identifiers.

    3. The third use is to embed different syntaxes into Lisp syntax. An old example for that is the infix read macro, which allows embedded infix expressions. Other examples are embedded HTML or XML syntax, or embedded fragments of other programming language syntaxes.

    4. Sometimes read macros are used to implement other (related) languages that use s-expression syntaxes that are different from the pre-defined Common Lisp syntax. An example would be a reader for Scheme s-expressions - which are slightly different from Common Lisp.