LISP:关键字参数,提供-p [英] LISP: Keyword parameters, supplied-p
问题描述
目前,我正在研究Peter Seibel的"Practical Common Lisp".
在实用:简单数据库"一章中( http://www.gigamonkeys.com/book/practical-a-simple-database.html )Seibel通过以下示例说明了关键字参数和提供的参数的用法:
(defun foo (&key a (b 20) (c 30 c-p)) (list a b c c-p))
结果:
(foo :a 1 :b 2 :c 3) ==> (1 2 3 T)
(foo :c 3 :b 2 :a 1) ==> (1 2 3 T)
(foo :a 1 :c 3) ==> (1 20 3 T)
(foo) ==> (NIL 20 30 NIL)
因此,如果我在参数列表的开头使用& key,则可以使用3个参数名称,默认值和第三个参数(如果没有提供)的列表.好的. 但是,请看上面示例中的代码:
(list a b c c-p)
lisp解释器如何知道c-p是我的提供的参数"?
让我们重新缩进函数 foo :
(defun foo (&key a
(b 20)
(c 30 c-p))
(list a b c c-p))
如果像这样缩进,您将看到该函数具有三个关键字参数:a,b和c.这些在函数主体中可用.
对于关键字参数 c ,有一个声明为 cp 的变量,该变量将为T或NIL,这取决于在获取foo时是否已传递 c 叫.
通常可以将关键字参数声明为以下选项之一:
- 作为单个变量名称
- 变量名和默认值的列表
- 变量名,默认值和变量的列表,这些列表将显示调用函数时是否已传递参数
当要查看值是来自调用还是默认值时,提供的-p尤其有趣:
(defun make-my-array (size &key (init-value nil init-value-supplied-p))
(if init-value-supplied-p
(make-array size :initial-element init-value)
(make-array size)))
现在用户可以将元素初始化为NIL:
(make-my-array 10 :init-value nil)
此处,默认值和提供的值都可以为NIL,但是我们需要有所不同.通过变量 init-value-supplied-p ,可以查看变量 init-value 的NIL值是来自默认值还是来自函数调用. >
At the moment I'm working through "Practical Common Lisp" from Peter Seibel.
In the chapter "Practical: A Simple Database" (http://www.gigamonkeys.com/book/practical-a-simple-database.html) Seibel explains keyword parameters and the usage of a supplied-parameter with the following example:
(defun foo (&key a (b 20) (c 30 c-p)) (list a b c c-p))
Results:
(foo :a 1 :b 2 :c 3) ==> (1 2 3 T)
(foo :c 3 :b 2 :a 1) ==> (1 2 3 T)
(foo :a 1 :c 3) ==> (1 20 3 T)
(foo) ==> (NIL 20 30 NIL)
So if I use &key at the beginning of my parameter list, I have the possibility to use a list of 3 parameters name, default value and the third if the parameter as been supplied or not. Ok. But looking at the code in the above example:
(list a b c c-p)
How does the lisp interpreter know that c-p is my "supplied parameter"?
Let's reindent the function foo:
(defun foo (&key a
(b 20)
(c 30 c-p))
(list a b c c-p))
If you indent it like this you will see that the function has three keyword parameters: a, b and c. These are available in the body of the function.
For the keyword parameter c there is a variable declared c-p that will be T or NIL depending whether c has been passed when foo gets called.
A keyword parameter generally can be declared as one of the following options:
- as a single variable name
- a list of a variable name and a default value
- a list of a variable name, a default value and a variable that will show whether the parameter has been passed or not when the function gets called
The supplied-p is particularly interesting when one wants to see whether the value comes from the call or the default value:
(defun make-my-array (size &key (init-value nil init-value-supplied-p))
(if init-value-supplied-p
(make-array size :initial-element init-value)
(make-array size)))
Now the user can init the elements to NIL:
(make-my-array 10 :init-value nil)
Here the default value and the supplied value can both be NIL, but we need to make a difference. The variable init-value-supplied-p makes it possible to see whether the NIL value of the variable init-value comes from the default or from the function call.
这篇关于LISP:关键字参数,提供-p的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!