为什么 Common Lisp 中冒号位于变量之前
Common Lisp 中变量前面的冒号语法是什么意思?我见过这样的程序,我将在这里从大量函数中展示一些示例代码。
(defun expand (successorf node)
(mapcar (lambda (action-state-cost)
(let ((action (car action-state-cost))
(state (cadr action-state-cost))
(cost (caddr action-state-cost)))
(make-node :state state :parent node
:action action :path-cost (+ (node-path-cost node) cost)
:depth (1+ (node-depth node)))
))
(funcall successorf (node-state node))
))
What does the syntax, colons preceding variable in Common Lisp, mean? I've seen programs with such, and I'll present some sample code here, out of a large set of functions.
(defun expand (successorf node)
(mapcar (lambda (action-state-cost)
(let ((action (car action-state-cost))
(state (cadr action-state-cost))
(cost (caddr action-state-cost)))
(make-node :state state :parent node
:action action :path-cost (+ (node-path-cost node) cost)
:depth (1+ (node-depth node)))
))
(funcall successorf (node-state node))
))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关键字符号
:foo
是一个关键字符号。KEYWORD
包中并从其导出,用法
当需要以下属性的组合时,使用关键字符号:
KEYWORD
KEYWORD
:foo
比':foo
更好,:foo
计算为:foo
本身,并且仅计算为:foo
在 Common Lisp 中,符号通常可以位于包中(一种命名空间)。
包
foo
中未导出的符号bar
写为foo::bar
。双冒号位于包名称和符号名称之间。导出的符号将写为
foo:bar
。使用单个冒号。如果该符号在当前包中可用,则在没有包的情况下写为
bar
。包
KEYWORD
有一个特殊的包叫做
KEYWORD
。该包中的符号bar
简单且始终写为:bar
。示例
这些关键字符号还具有这些有趣的属性:这些符号会自动从包
KEYWORD
中导出(因此keyword::bar
,keywords:bar
、::bar
和:bar
都是相同的符号)并且它们对自身求值:用法
关键字符号例如用作命名参数中的名称:
通常它们也用于实例和结构构造的参数。
DEFSTRUCT
是一个 Common Lisp 宏,它生成多个函数。其中之一是函数MAKE-NODE
,它可以用作:注意:有时数据也可能是关键字。例如,在上面的形式中,状态可能是
:open
而不是open
:Keyword Symbols
:foo
is a keyword symbol.KEYWORD
packageUsage
Keyword symbols are used when one needs the combination of the following properties:
KEYWORD
KEYWORD
:foo
better than':foo
:foo
evaluates to:foo
itself and only to:foo
In Common Lisp generally symbols can be in a package (kind of a namespace).
An unexported symbol
bar
in a packagefoo
is written asfoo::bar
. The double colon is between the package name and the symbol name.An exported symbol then is written as
foo:bar
. A single colon is used.If the symbol is available in the current package then is written as
bar
without the package.The package
KEYWORD
There is a special package called
KEYWORD
. A symbolbar
in that package is simply and always written as:bar
.Examples
These keyword symbols have also these interesting properties: the symbols are automatically exported from the package
KEYWORD
(sokeyword::bar
,keyword:bar
,::bar
and:bar
are all the same symbol) and they evaluate to themselves:Usage
Keyword symbols are used for example as names in named arguments:
Typically they are also used in arguments to instance and structure construction.
DEFSTRUCT
is a Common Lisp macro and it generates several functions. One of them is a functionMAKE-NODE
, which can be used as:Note: sometimes the data might also be a keyword. For example in above form, the state might be
:open
and notopen
:实际上,它们不是变量;而是变量。这些是关键词。它们是一种特殊的高效标记,类似于其他语言中的“原子”。这是一种方便的内置方法,可将命名(并且几乎总是可选)参数传递到函数调用中。
http://www.gigamonkeys.com/book/functions.html 描述了以下语法函数调用。
They're not variables, actually; those are keywords. They're a special kind of efficient token, similar to “atoms” in other languages. It's a convenient, built-in way to pass named (and, almost always, optional) parameters into a function call.
http://www.gigamonkeys.com/book/functions.html describes the syntax of function calls.