Reputation: 19
I have a question about lisp macros. I want to check a condition and, if it's true, to run a few things, not only one. The same for the false way. Can anybody please help??
(defmacro test (condition (&rest then) (&rest else))
`(if ,condition (progn ,@then) (progn ,@else)))
(test (= 4 3)
(print "YES") (print "TRUE")
(print "NO") (print "FALSE"))
Upvotes: 1
Views: 166
Reputation: 60014
The usual way to test macros is to use macroexpand
.
Your test case misses parens (lisp reader does not care about whitespace, such as line breaks and indentation):
(macroexpand '(test (= 4 3)
(print "YES") (print "TRUE")
(print "NO") (print "FALSE")))
Error while parsing arguments to DEFMACRO TEST:
too many elements in
((= 4 3) (PRINT "YES") (PRINT "TRUE") (PRINT "NO") (PRINT "FALSE"))
to satisfy lambda list
(CONDITION (&REST THEN) (&REST ELSE)):
exactly 3 expected, but got 5
while
(macroexpand '(test (= 4 3)
((print "YES") (print "TRUE"))
((print "NO") (print "FALSE"))))
(IF (= 4 3)
(PROGN (PRINT "YES") (PRINT "TRUE"))
(PROGN (PRINT "NO") (PRINT "FALSE")))
Please note that CL has cond
which is usually used when one needs multiple forms in
if
clauses.
Upvotes: 2