Notes on Lisp symbols

Copyright Gunnar Gotshalks -- Last Modified 2007 September 11

Components of a symbol

Reference: Patrick R. Harrison, Common Lisp & Aritificial Intelligence, Prentice-Hall, 1990, pp30..38.

Symbol names can contain any symbols except "(", ")", "'", '"' and ";". They cannot be numbers. This is similar to Postscript where the excluded characters are "(", ")" and "/". Actually "'" can be used in certain combinations but it is easy to make mistakes and so it is better to avoid it. Similarily it is best to avoid "." as a part of a symbol name.

Symbols are implemented as a record structure containing the following five fields. You can modify each field with different instructions. Each field can be accessed with the function name shown in the examples. The examples assume the symbol SYM has been created using a SET, SETQ or SETF command.

Creating symbols

The most common way of creating symbols is by using the following functions.

Property Lists

A property list has the following EBNF structure.
    PropertyList ::=  ( nil , aPropName aPropValue PropertyList)
    aPropName    ::= is the name of a property
    aPropValue   ::= any s-expression
The following are examples. The odd positions are the name of the property and the even positions are the value associated with the property to the left. Note in the second example that a property value can itself be a property list. When property lists are associated with a symbol (using SETF) the property values can be extracted using (GET symbol propName). Where "symbol" evaluates to a symbol and "propName" evaluates to the name of a property. The expression (GET symbol propName) evaluates to a place (a memory address) which in a print command means print the contents of, and in a SETF command means store in this location. Try the following.
    (setf (get 'x 'change) '(penny 3 dime 4 looney 6))
    (symbol-plist 'x)
    (get 'x 'change)
    (first (rest (get 'x 'change)))
Property lists can also be values of symbols. In this case use GETF to access a property value. Try the following, notice the symbol name is not quoted as GETF is similar to SETQ in this respect.
    (setq  x '(change (penny 3 dime 4 looney 6)))
    (getf x 'change)
    (first (rest (getf x 'change)))

Unlike GET, GETF does not expect a symbol as the first parameter.

    (get '(a 3 b 4 colour red c 5) 'colour)  ;; fails
    (getf '(a 3 b 4 colour red c 5) 'colour)  ;; returns  red 

Set a property list to nil use the following.

    (setf (symbol-plist 'symbol) nil)

Set a property value to nil use the following.

    (setf (get 'symbol 'propName) nil)

Association Lists

Association lists are like property lists except that propertyName-value pairs are paired in a list of length two; as in the following.
    ((colour black) (size large))
The above association list can be created using cons as in the following.
    (cons
      (cons 'colour (cons 'black nil))
      (cons (cons 'size (cons 'large nil)) nil))