Copyright Gunnar Gotshalks -- Last Modified 2007 September 11
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.
(setq x 3) (setq y 5) (+ (symbol-value 'x) (symbol-value 'y))
(setq balance 123) (symbol-name 'balance)
(setq aFunction 4) (defun aFunction (x y) (+ x (car y))) (symbol-value 'aFunction) (symbol-function 'aFunction)
(symbol-package 'aFunction)
(setf (get 'aFunction 'colour) 'red) (symbol-plist 'aFunction) (setf (get 'aFunction 'size) 'large) (symbol-plist 'aFunction)
(setq x '(a b c)) ; Create a list of three potential symbol names. (set (car x) 5) ; Create the symbol A and assign it the value 5. (symbol-value 'a) ; See it is created and set a value.
(setq y 'a) (symbol-value 'y) (setq a 1 b 2 c 3)
(setf x 4) (setf a 10 b 20 c 30)SETF directly changes the resulting memory location. In the following using SET a value is assigned to A (the first symbol on the list X).
(setq x '(a b c)) (set (car x) 5) ;; Symbol A has the value 5Now repeat the above using SETF instead of SET, and 20 instead of 5. Notice the list X is changed but not the value of A.
(setq x '(a b c)) (setf (car x) 20) ;; x becomes (20 b c)So in Wilensky, Chapter 7 the following is used to assign to the symbol "aFunction" the property "colour" with value "red".
(setf (get 'aFunction 'colour) 'red)The expression "(get 'aFunction 'colour)" internally returns a pointer to a location in the property list for "aFunction" where the property "colour" is or can be stored. SETF then uses that location to store the value "red".
(setq x '(a b c)) (a b c) (setq y (cons (car x) (cdr x))) (a b c) x (a b c) y (a b c) (setf (car x) 5) 5 x (5 b c) y (a b c) (setf (cddr x) 20) 20 x (5 b . 20) y (a b . 20)
(defun copy(x) (cond ((atom x) x) (t (cons (copy (car x)) (copy (cdr x)))) ))
PropertyList ::= ( nil , aPropName aPropValue PropertyList) aPropName ::= is the name of a property aPropValue ::= any s-expressionThe 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.
(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)
((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))