31st
2009
Jan
2009
Macro Patterns
I was talking about Lisp macros with Kyle and we decided to write down the patterns of different macros we’ve seen and what they allow you to do, with the intent of teaching others.
Interesting things you can do with macros:
- resource management (e.g. with-open-file)
- behavior injection à la AOP (e.g. memoization, debug printing)
- alternate control-flow constructs
- alternate looping constructs
- alternate binding constructs (e.g. destructuring-bind, cut)
- lazy evaluation
- DSLs
- code generation
Things that macros actually give you that you can’t do otherwise:
- programmatic binding
- non-hygienic binding
- controlled evaluation
- reifying the (calling) source code
If you’ve seen other types of macros or know of good examples of the above, please comment. I will try to update this list as we come up with more.