Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
5  
Before voting to close please allow me (or even suggest how) to improve the question. I want to know what macros are good for and how I should learn them. It is a very specific question for which I expect a very specific answer, such as: write a small compiler or whatever application LISP macros are good for. –  Giorgio Jul 3 '12 at 20:09
5  
+1 for you and -1 for p.se for being necessary to pre-emptively defend a question from the "close happy"... –  bunglestink Jul 3 '12 at 22:29
    
    
+1 This is a good question, especially the "why use macros instead of functions" part. I'd really like to see more answers that address that. –  Matt Fenwick Oct 24 '12 at 22:26
add comment

2 Answers

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 it's usefullness 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 to slow, without code-generation it wouldn't be executable.

Another fine example is the binary serialization practical of the Practical Common Lisp-book: http://www.gigamonkeys.com/book/practical-parsing-binary-files.html

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
add comment

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
add comment

Your Answer

 
discard

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.