An association list is any list of the following form:
The keys should be atoms. Following each key, you can put any sequence of LISP expressions.((<key1> ...<expressions>) (<key2> ...<expressions>)....)
Use the interpreter to enter this example of an association list:
LISP provides a function, assoc, to retrieve information easily from association lists given a retrieval key.>(setf person1 '((first-name john) (last-name smith) (age 23) (children jane jim))) ((FIRST-NAME JOHN) (LAST-NAME SMITH) (AGE 23) (CHILDREN JANE JIM))
For example:
Notice that assoc returns the entire key-expression sublist. It does not matter to assoc what order the keys appear in the association list or how many expressions are associated with each key.>(assoc 'age person1) (AGE 23) >(assoc 'children person1) (CHILDREN JANE JIM)
Setf can be used to change particular values. For example, here is a function that can be used on a birthday to update a person's age automatically.
Have your LISP interpreter evaluate this definition, then see that it works:(defun make-older (person) (setf (second (assoc 'age person)) (1+ (second (assoc 'age person)))))
Assoc will return nil if the key is not found.>(make-older person1) 24 >(assoc 'age person1) (AGE 24)
But it is very easy to add new key-expression sublists, again using setf.>(assoc 'sex person1) NIL
>(setf person1 (cons '(sex male) person1)) ((SEX MALE) (FIRST-NAME JOHN) (LAST-NAME SMITH) (AGE 24) (CHILDREN JANE JIM))
© Colin Allen & Maneesh Dhagat