I learned that it is easy to define a small macro in scheme by syntax-rules.
Is it possible to define a syntax-rules macro which will return a list can be read by defmacro in Common Lisp?
Which might work like this:
(defmacro and (&rest rest)
(syntax-rules (rest) ()
((_) t)
((_ x) x
((_ x y) (if x (if y y nil) nil))
((_ x y ...) (if x (and y ...) nil))))
Tim Bradshaw's
destructuring-matchis intended to make this kind of pattern matching easy in macros: it is a combination ofcaseanddestructuring-bind, so the key of each clause has the syntax of a destructuring lambda list. There are several other pattern matching systems for CL, but this one explicitly is aimed at writing macros: it's not a general data structure matching tool, just a generalised version ofdestructuring-bind.It comes with a little example macro (itself written using
destructuring-matchas well as expanding into something which usesdestructuring-match!) calleddefine-matching-macro, in which you can write a version ofand, here calledet. I think the following two macros are correct but I have not thought much about them.You can alternatively just use
destructuring-matchin an ordinarydefmacromacro. Here I am not using&wholeto get the whole form so we don't have to bother with thecarof the form:Notes:
destructuring-matchuses symbols whose name is"_"as blanks the same way Scheme does, so you don't need to declare them ignored, they are all distinct from each other, and in fact you may not refer to them in the body of the clause; but it does not do Scheme'sx ...thing at all, so you just need to use dotted lambda lists or&restor whatever you prefer;(if x x nil), as well as being redundant (it's justx) may evaluatextwice, which is unsafe.