When you're dealing with lisp, sooner or later, you're going to hear about macros, and how utterly, astoundingly fantastic they are.
Macros are for:
- Creating new operators and saving you a gang of time by "extending the language"
Um, that's all I can think of. There is probably a lot more to it than that, but if you're in desperate need of a starting point, that would probably be quite a goody.
Of course, when you're a total monkey like me, you very quickly pick up on the emotional content of any essays,blogs or books, but the logic escapes you for a bit.
Even when you think you've wrapped your brain around the issue, you still have this nagging feeling that you're not getting something vital.
A few things fell nicely into place when I was reading about anaphoric macros. I think they should be used to introduce the idea of lisp macros, rather than being interesting items you only get to see after a while...
So let's look at AIF. AIF is the anaphoric version of if. Anaphoric meaning something like "referring to itself". You can define and test it like this:
(defmacro aif (test-form then-form &optional else-form)
`(let ((it ,test-form))
(if it ,then-form ,else-form)))
(defun big-long-calculation ()
t)
(aif (big-long-calculation)
(format t "hello"))
;running the above will give you "hello" in the repl.
;changing big-long-calculation to return nil will result
;in the aif test doing nothing.
Which allows you to avoid this:
(let ((result (big-long-calculation)))
(if result
(foo result)))
... and use this instead:
(aif (big-long-calculation)
(foo it))
(all this stuff stolen from Paul Graham's "On Lisp" - fantastic book that I've not finished reading, because it's too advanced, or I'm too much of a monkey)
... so you've "extended" the language by adding a new operator, as elemental as, say, "+" or "format" (I think).
Which is pretty powerful.