← Back to context

Comment by kazinator

3 months ago

Lisps have unnterned symbols also.

Interning is important for symbols that are involved in I/O: being printed and read, so that two or more occurrences of the same symbol in print will all read to the same object, and so there is print-read consistency: we can print an interned symbol, and then read the printed symbol to obtain the same object.

Symbols are useful without this also. Symbolic processing that doesn't round trip symbols to a printed notation and back doesn't require interned symbols.

Symbols have a name, but are not that name.

They also have various properties, which depends on the Lisp dialect.

Classical Lisp dialects, like MacCarthy's original, endow each symbol with a property list. Another classical property is the "value cell".

In Common Lisp lists have a home package retrieved by the function symbol-package.

A variable being globally proclaimed special can be implemented as a property of the symbol.

Symbols are interned in packages, not globally, so two symbols can be interned, yet have the same name: mypackage:let and cl:let both have the name "LET", but different home packages.

Uninterned symbols with the same name can be readily made: just call (make-symbol "FOO") twice and you get two symbols named "FOO", which print as #:FOO.

The #: notation means symbol with no home package, used as a proxy for "uninterned", though a perverse situation can be contrived whereby a symbol has no home package (and so prints with the #: notation), yet is interned into a package.

Introduce a FOO symbol in the keyword package:

  [1]> :foo
  :FOO

Now import it into the CL-USER package:

  [2]> (import :foo :cl-user)
  T

Verify that cl-user::foo is actually the keyword symbol :foo:

  [3]> 'cl-user::foo
  :FOO

Now, unintern :foo from the keyword package, leaving it homeless:

  [4]> (unintern :foo :keyword)
  T

Let's print it, accessing it via the cl-user package where it is still interned by import:

  [5]> 'cl-user::foo
  #:FOO

There is quite a bit to this symbol stuff than just "interned strings".

Symbols are simply not strings objects; they have strings as a name.