2017-03-03 37 views
2

我试图创建一个具有默认值&optional参数的lisp宏。不幸的是,参数的处理方式有所不同,具体取决于它是从默认值还是从提供给宏的参数中读取。下面的代码片段重现该问题:&lisp宏中的可选参数:为什么此变量的行为如此?

(setf table1 '((1 2 3) 
       (4 5 6)) 
     table2 '((10 20 30) 
       (40 50 60))) 

(defmacro test-lambda (f &optional (tableau table1)) 
    `(list ,f ,tableau)) 

? (test-lambda 0 table2) ;; This works... 
(0 ((10 20 30) (40 50 60))) 

? (test-lambda 0)   ;; ...but this doesn't 
> Error: Car of ((1 2 3) (4 5 6)) is not a function name or lambda-expression. 
> While executing: CCL::CHEAP-EVAL-IN-ENVIRONMENT, in process listener(1). 
> Type :POP to abort, :R for a list of available restarts. 
> Type :? for other options. 
1 > 

我不太明白为什么宏观不会在第二种情况下,默认值工作。有没有更好的方法来编码或至少是一种解决方法?

感谢,

回答

6

什么

您需要引用,默认参数值:

(defmacro test-lambda-1 (f &optional (tableau 'table1)) 
    `(list ,f ,tableau)) 
(test-lambda-1 0) 
==> (0 ((1 2 3) (4 5 6))) 

为什么

你需要考虑how Common Lisp evaluates your code:当它看到(test-lambda ...),它

让我们试试吧:

(macroexpand '(test-lambda 0)) 
==> (LIST 0 ((1 2 3) (4 5 6))) ; T 
(macroexpand '(test-lambda-1 0)) 
==> (LIST 0 TABLE1) ; T 
(macroexpand '(test-lambda 0 table2)) 
==> (LIST 0 TABLE2) ; T 
(macroexpand '(test-lambda-1 0 table2)) 
==> (LIST 0 TABLE2) ; T 

现在你可以看到那里的错误是来自:你没有报价参数的默认值,所以它得到了两次评估。

相关问题