为什么 Common Lisp 中的包名称和导出使用未驻留的符号?

发布于 2024-12-12 01:47:46 字数 798 浏览 0 评论 0原文

截屏 (存档博客)在 Common List 上,作者 (Alexander Lehmann) 使用未驻留的符号作为包名称和导出。

(defpackage #:foo
  (:use :cl)
  (:export #:bar
           #:baz))

(in-package #:foo)

他还在匿名函数前面使用了尖号。

(defun transposed (m)
  (make-instance 'matrix
                 :rows (matrix-cols m)
                 :cols (matrix-rows m)
                 :generator #'(lambda (i j) (matrix-at m j i))))

Practical Common Lisp一书中,尖号不用于包名称和导出我读过。

在这些情况下使用未驻留符号(锐号)的原因是什么?

In a screen cast (archived blog) on Common List the author (Alexander Lehmann) uses uninterned symbols for package names and exports.

(defpackage #:foo
  (:use :cl)
  (:export #:bar
           #:baz))

(in-package #:foo)

He also uses the sharp sign in front of anonymous functions.

(defun transposed (m)
  (make-instance 'matrix
                 :rows (matrix-cols m)
                 :cols (matrix-rows m)
                 :generator #'(lambda (i j) (matrix-at m j i))))

In the book Practical Common Lisp the sharp sign isn't used for package names and exports as far as I have read.

What's the reason for using the uninterned symbols (the sharp sign) in these cases?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

自此以后,行同陌路 2024-12-19 01:47:46

使用驻留符号会污染您当前所在的包,这些符号仅用于其名称:

[1]> *package*
#<PACKAGE COMMON-LISP-USER>
[2]> (defpackage bar)
#<PACKAGE BAR>
[3]> (find-symbol "BAR")
BAR ;
:INTERNAL

未驻留符号不会这样做:

;; Uninterned symbols don't cause symbol pollution:
[4]> (defpackage #:foo)
#<PACKAGE FOO>
[5]> (find-symbol "FOO")
NIL ;
NIL

您也可以直接使用字符串,但由于您通常处理大写符号名称,它们写起来不太方便:

[6]> (defpackage "BARFOO")
#<PACKAGE BARFOO>
[7]> (find-symbol "BARFOO")
NIL ;
NIL

示例

为了说明问题,请考虑以下交互:

[1]> (defpackage hello (:use cl) (:export hello))
#<PACKAGE HELLO>

;; Let's write some FOO stuff...
[2]> (defpackage foo (:use cl))
#<PACKAGE FOO>
[3]> (in-package foo)
#<PACKAGE FOO>

;; Oh, I forgot to import HELLO!
;; Let's fix that.
FOO[4]> (defpackage foo (:use cl hello))
*** - (COMMON-LISP:USE-PACKAGE (#<PACKAGE HELLO> #<PACKAGE COMMON-LISP>)
      #<PACKAGE FOO>): 1 name conflicts remain
      Which symbol with name "HELLO" should be accessible in #<PACKAGE FOO>?

;; Oops.

Using an interned symbol pollutes the package you're currently in with symbols that are only used for their names anyway:

[1]> *package*
#<PACKAGE COMMON-LISP-USER>
[2]> (defpackage bar)
#<PACKAGE BAR>
[3]> (find-symbol "BAR")
BAR ;
:INTERNAL

Uninterned symbols don't do that:

;; Uninterned symbols don't cause symbol pollution:
[4]> (defpackage #:foo)
#<PACKAGE FOO>
[5]> (find-symbol "FOO")
NIL ;
NIL

You can also use strings directly, but since you're usually dealing with uppercase symbol names, they are less convenient to write:

[6]> (defpackage "BARFOO")
#<PACKAGE BARFOO>
[7]> (find-symbol "BARFOO")
NIL ;
NIL

Example

To illustrate the problem, consider the following interaction:

[1]> (defpackage hello (:use cl) (:export hello))
#<PACKAGE HELLO>

;; Let's write some FOO stuff...
[2]> (defpackage foo (:use cl))
#<PACKAGE FOO>
[3]> (in-package foo)
#<PACKAGE FOO>

;; Oh, I forgot to import HELLO!
;; Let's fix that.
FOO[4]> (defpackage foo (:use cl hello))
*** - (COMMON-LISP:USE-PACKAGE (#<PACKAGE HELLO> #<PACKAGE COMMON-LISP>)
      #<PACKAGE FOO>): 1 name conflicts remain
      Which symbol with name "HELLO" should be accessible in #<PACKAGE FOO>?

;; Oops.
面犯桃花 2024-12-19 01:47:46

#'function 运算符的简写(在 Practical Common Lisp 书中多次使用)。

#' is a shorthand for the function operator (this is used a few times in the Practical Common Lisp book).

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