我有一个关于lisp宏的问题。我想检查一个条件,如果它是真的,运行几个东西,而不是只运行一个。对于错误的方式也是如此。有人能帮帮忙吗??
(defmacro test (condition (&rest then) (&rest else))
`(if ,condition (progn ,@then) (progn ,@else)))
(test (= 4 3)
(print "YES") (print "TRUE")
(print "NO") (print "FALSE"))发布于 2020-08-01 02:44:07
测试宏的常用方法是使用macroexpand。
您的测试用例缺少括号(lisp阅读器不关心空格,如换行符和缩进):
(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而
(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")))请注意,CL有cond,通常在if子句中需要多种形式时使用。
https://stackoverflow.com/questions/63196643
复制相似问题