Property Lists
(from the Norvig and Graham texts)

In Lisp, symbols are actually stored as structures with the following fields:
1. name (like 'FOO)
2. package (we won't study this now)
3. value (like 27)
4. function  (like "length")
5. plist --> property list

Using property lists:

In Common Lisp, every symbol has a property-list, or p-list.
Initially, the key "alizarin" has no value for a "color" property:

>(get 'alizarin 'color)  ;; a property defaults to NIL
NIL

To associate a value with a key, use setf with a get:
>(setf (get 'alizarin 'color) 'red)
RED

>(get 'alizarin 'color)
RED

Now the color property of alizarin is red.

Define a function putprop to assign a value to a key:

>(defun putprop (object value field)
   (setf (get object field) value))

>(putprop 'alizarin 'red 'color)


Here we define Patrick's parents as Robert and Dorothy:
>(get 'patrick 'parents)
NIL

>(setf (get 'patrick 'parents) '(robert dorothy))
(ROBERT DOROTHY)

>(get 'patrick 'parents)
(ROBERT DOROTHY)

OR USE:
>(putprop 'patrick '(robert dorothy) 'parents)


Another example, put bread and butter in as contents of the bag:
>(putprop 'bag '(bread butter) 'contents)

>(get 'bag 'contents)
(BREAD BUTTER)

Exercises

1. Use setf and get to assign these properties to Mary: age 45, job = Banker, sex = female, and children = Bonnie and Bob 2. Use the putprop function to assign these same values. 3. Use the symbol-plist function to print out the properties of Mary.

Answers


1.  (setf (get 'mary 'age) 45)
    (setf (get 'mary 'sex) 'female)
    (setf (get 'mary 'banker) 'job)
    (setf (get 'mary 'children) '(bonnie bob))

2. (putprop 'mary 45 'age)
   (putprop 'mary 'banker 'job)
   (putprop 'mary 'female 'sex)
   (putprop 'mary '(bonnie bob) 'parents)

3. (symbol-plist 'mary)  prints all the properties of mary
   (parents (bonnie bob) sex female job banker age 45)

TRY IT OUT!!