Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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))))

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?

like image 372
Jan Deinhard Avatar asked Oct 25 '11 19:10

Jan Deinhard


1 Answers

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.
like image 187
Matthias Benkard Avatar answered Nov 04 '22 19:11

Matthias Benkard