[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Using Macros in Lisp
- To: Gallagher@cs.umass.edu
- Subject: Using Macros in Lisp
- From: firstname.lastname@example.org (Mark Rosenstein)
- Date: Thu, 7 Mar 91 08:57:46 -0500
- Cc: email@example.com
- In-reply-to: Kevin Gallagher's message of Wed, 6 Mar 91 13:54:09 EST <2877274449-2458510@Gilgamesh>
Like they used to say in high school shop, "use the right tool for the
job." When permuting syntax is the right thing, macros are the right
Date: Wed, 6 Mar 91 13:54:09 EST
From: Kevin Gallagher <Gallagher@gilgamesh.cs.umass.edu>
I have just a small nit:
Unfortunately, I don't know of any text that describes writing anything
other than trivial macros. Probably, the best thing to do is to look at
code written by good lisp programmers. Important things to consider when
writing macros are:
-- Take care with local variables introduced by the macro.
(i.e., use gensyms for any new locals)
Actually, I think using make-symbol generates easier to understand
macro expansions, eg.:
(defmacro too-cool-for-school (&body somebody)
(let ((new-local (make-symbol "SOME-NAME-WITH-MEANING")))
`(let ((,new-local (something-wonderful)))
-- Take care to preserve the left to right order of argument
-- Don't evaluate argument forms more than once.
-- Know the difference between read time, macro expansion time,
compile time, and load time.
-- Don't do any side effects in your macro. A macro is just a source
to source transformation.
-- Make sure the macroexpansion doesn't cause spurious compiler
This can sometimes be tricky with a good optimizing compiler, but
a reasonable requirement.
One of the big tricks is to sketch out what you want the macro
expansion to be, before you start getting into the backquotes,
and comma at-signs. Also try to do most of your syntax twiddling
in a function that the macro calls at expand time.
#define is to lisp macros
as C is to lisp
(even though this sounds cool, it probably isn't right, oh well)