An alternative way to attach data to symbols is to use Common LISP's property list feature. For each symbol, the LISP interpreter maintains a list of properties which can be accessed with the function get.
Get expects to be given a symbol and a key. If a value has been set for that key, it is returned, otherwise get returns nil.
As this example shows, the value to be returned by a get expression is set using setf. (This is another place where setf works but setq will not.) The way to think of setf's behavior here is that you tell it exactly what you will type and what should be returned. The example here assigns the value 45 as the value to be returned when you type (get 'mary 'age).>(get 'mary 'age) NIL >(setf (get 'mary 'age) 45) 45 >(get 'mary 'age) 45
Additional properties can be added in the same way.
If, for some reason, you need to see all the properties a symbol has, you can do so:>(setf (get 'mary 'job) 'banker) BANKER >(setf (get 'mary 'sex) 'female) FEMALE >(setf (get 'mary 'children) '(bonnie bob)) (BONNIE BOB)
Get is a convenient way to manage related data, and in some ways is more flexible than assoc. For many applications, however, it is necessary to build more sophisticated data structures. Options for doing this include customized nested list structures, arrays, and use of the Common Lisp Object System (CLOS). Arrays, vectors, and strings will be introduced in the rest of this chapter. Customized list structures will not be covered here because they are highly dependent on the specific application. CLOS will not be covered here, either, as the purpose of this book is to bring you to a point where you can easily understand other presentations of advanced LISP topics.>(symbol-plist 'mary) (SEX FEMALE JOB BANKER AGE 45 CHILDREN (BONNIE BOB))
© Colin Allen & Maneesh Dhagat