Many examples of macros seem to be about hiding lambdas, e.g. with-open-file in CL. I’m looking for some more exotic uses of macros, particularly in PLT Scheme. I’d like to get a feel for when to consider using a macro vs. using functions.
Many examples of macros seem to be about hiding lambdas, e.g. with-open-file in CL.
Share
I only use Scheme macros (
define-syntax) for tiny things like better lambda syntax:Which lets you write
Dan Friedman has a mind-bending implementation of OO using macros: http://www.cs.indiana.edu/~dfried/ooo.pdf
But honestly, all the useful macros I’ve defined are stolen from Paul Graham’s On Lisp and are generally easier to write with
defmacro(define-macroin PLT Scheme). For example,aifis pretty ugly withdefine-syntax.define-syntaxis odd in that it’s only easy to use for very simple macros, where you are glad of the inability to capture variables; and very complicated macro DSLs, where you are glad of the inability to capture variables easily. In the first case you want to write the code without thinking about it, and in the second case you have thought enough about the DSL that you are willing to write part of it in thesyntax-rules/syntax-caselanguage which is not Scheme in order to avoid mystifying bugs.But I don’t use macros that much in Scheme. Idiomatic Scheme is so functional that many times you just want to write a functional program and then hide a few lambdas. I got on the functional train and now believe that if you have a lazy language or a good syntax for lambda, even that isn’t necessary, so macros are not all that useful in a purely functional style.
So I’d recommend Practical Common Lisp and On Lisp. If you want to use PLT Scheme, I think most of their
defmacromacros will work withdefine-macro. Or just use Common Lisp.