All Manuals > LispWorks® User Guide and Reference Manual > 47 The SYSTEM Package

cdr-assoc Accessor

Summary

A generalized reference for alist elements.

Package

system

Signatures

cdr-assoc item alist &key test test-not key => result

setf (cdr-assoc item place &key test test-not key) value => value

Arguments
item
An object.
alist
An association list.
test
A function designator.
test-not
A function designator.
key
A function designator.
place
A setf place containing an association list.
value
An object.
Values
result
An object (from alist) or nil.
value
An object.
Description

The accessor cdr-assoc provides a generalized reference for elements in an association list. The arguments are all as specified for the Common Lisp function assoc. cdr-assoc and its setf expander read and write the cdr of an element in a manner consistent with the Common Lisp notion of places.

cdr-assoc returns the cdr of the first cons in the alist alist that matches item (tested using test, test-not and key as for assoc), or nil if no element of alist matches.

Using cdr-assoc with setf modifies the first cons in the value of place that matches item, setting its cdr to value. If no element matches, then it pushes the value of (cons item value) onto the value of place and sets place to this new alist. This is similar to how cl:getf is defined.

When place is a globally accessible place that may be read by another thread without synchronization (by a lock or other synchronization mechanism), you need to wrap alist by globally-accessible. See 19.3.4 Making an object's contents accessible to other threads for a discussion.

Examples
CL-USER 1 > (defvar *my-alist*
              (list (cons :foo 1)
                    (cons :bar 2)))
*MY-ALIST*
 
CL-USER 2 > (setf (sys:cdr-assoc :bar 
                                 *my-alist*) 3)
3
 
CL-USER 3 > *my-alist*
((:FOO . 1) (:BAR . 3))

LispWorks® User Guide and Reference Manual - 01 Dec 2021 19:31:02