为什么 Common Lisp 中冒号位于变量之前

发布于 2024-12-21 21:26:49 字数 562 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸印温柔 2024-12-28 21:26:50

关键字符号

:foo 是一个关键字符号

  • 驻留在 KEYWORD 包中并从其导出,
  • 不断绑定到自身

用法

当需要以下属性的组合时,使用关键字符号:

  • 符号是正确的数据结构
  • 符号相同的名称应该是唯一的(通过将它们实习在一个包中)-> package 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 都是相同的符号)并且它们对自身求值:

CL-USER 5 > :bar
:BAR

CL-USER 6 > (describe :bar)

:BAR is a SYMBOL
NAME          "BAR"
VALUE         :BAR
FUNCTION      #<unbound function>
PLIST         NIL
PACKAGE       #<The KEYWORD package, 0/4 internal, 5830/8192 external>

CL-USER 7 > (eq 'keyword::bar ':bar)
T

CL-USER 8 > (eq :bar ':bar)  ; quoted or unquoted, each subform evaluates to :bar
T

用法

关键字符号例如用作命名参数中的名称:

(defun foo (&key bar) (+ bar 10))

(foo :bar 7)

通常它们也用于实例和结构构造的参数。

(defstruct node state parent action)

DEFSTRUCT 是一个 Common Lisp 宏,它生成多个函数。其中之一是函数MAKE-NODE,它可以用作:

(make-node :state 'open
           :parent some-parent
           :action an-action)

注意:有时数据也可能是关键字。例如,在上面的形式中,状态可能是 :open 而不是 open

(make-node :state :open
           :parent some-parent
           :action an-action)

Keyword Symbols

:foo is a keyword symbol.

  • interned in and exported from the KEYWORD package
  • constantly bound to itself

Usage

Keyword symbols are used when one needs the combination of the following properties:

  • a symbol is the right data structure
  • symbols with the same name should be unique (by interning them in a package) -> package KEYWORD
  • different packages are not needed or wanted -> package KEYWORD
  • writing the symbol should be easy by not needing to quote them -> :foo better than ':foo
  • the ability to act as a variable with different values is not needed -> :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 package foo is written as foo::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 symbol bar 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 (so keyword::bar, keyword:bar, ::bar and :bar are all the same symbol) and they evaluate to themselves:

CL-USER 5 > :bar
:BAR

CL-USER 6 > (describe :bar)

:BAR is a SYMBOL
NAME          "BAR"
VALUE         :BAR
FUNCTION      #<unbound function>
PLIST         NIL
PACKAGE       #<The KEYWORD package, 0/4 internal, 5830/8192 external>

CL-USER 7 > (eq 'keyword::bar ':bar)
T

CL-USER 8 > (eq :bar ':bar)  ; quoted or unquoted, each subform evaluates to :bar
T

Usage

Keyword symbols are used for example as names in named arguments:

(defun foo (&key bar) (+ bar 10))

(foo :bar 7)

Typically they are also used in arguments to instance and structure construction.

(defstruct node state parent action)

DEFSTRUCT is a Common Lisp macro and it generates several functions. One of them is a function MAKE-NODE, which can be used as:

(make-node :state 'open
           :parent some-parent
           :action an-action)

Note: sometimes the data might also be a keyword. For example in above form, the state might be :open and not open:

(make-node :state :open
           :parent some-parent
           :action an-action)
最好是你 2024-12-28 21:26:50

实际上,它们不是变量;而是变量。这些是关键词。它们是一种特殊的高效标记,类似于其他语言中的“原子”。这是一种方便的内置方法,可将命名(并且几乎总是可选)参数传递到函数调用中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文