为什么在Common Lisp中将未隔离的符号用于程序包名称和导出? [英] Why are uninterned symbols used for package names and exports in Common Lisp?
问题描述
在常见清单"上的屏幕投射中,作者使用的是未实习生包裹名称和出口的符号.
In a screen cast on Common List the author 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))))
在 Practical Common Lisp 一书中,尖锐符号不用于包装名称,也不能用于导出据我所读.
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?
推荐答案
使用插入符号会反正以仅用于其名称的符号污染您当前所在的包裹:
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.
这篇关于为什么在Common Lisp中将未隔离的符号用于程序包名称和导出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!