CLCS
Macro

setf, psetf

setf {!pair}*{result}*

psetf {!pair}*nil

pair ::=place newvalue

Arguments and Values

placea place.
newvaluea form.
resultsthe multiple values_2 returned by the storing form for the last place, or nil if there are no pairs.

Description

setf changes the value of place to be newvalue.

(setf place newvalue) expands into an update form that stores the result of evaluating newvalue into the location referred to by place. Some place forms involve uses of accessors that take optional arguments. Whether those optional arguments are permitted by setf, or what their use is, is up to the setf expander function and is not under the control of setf. The documentation for any function that accepts &optional, &rest, or &key arguments and that claims to be usable with setf must specify how those arguments are treated.

If more than one pair is supplied, the pairs are processed sequentially; that is,

 (setf place-1 newvalue-1
       place-2 newvalue-2
       ...
       place-N newvalue-N)

is precisely equivalent to

 (progn (setf place-1 newvalue-1)
        (setf place-2 newvalue-2)
        ...
        (setf place-N newvalue-N))

For psetf, if more than one pair is supplied then the assignments of new values to places are done in parallel. More precisely, all subforms (in both the place and newvalue forms) that are to be evaluated are evaluated from left to right; after all evaluations have been performed, all of the assignments are performed in an unpredictable order.

For detailed treatment of the expansion of setf and psetf, see Kinds of Places.

Examples

 (setq x (cons 'a 'b) y (list 1 2 3)) ⇒  (1 2 3) 
 (setf (car x) 'x (cadr y) (car x) (cdr x) y) ⇒  (1 X 3) 
 x ⇒  (X 1 X 3) 
 y ⇒  (1 X 3) 
 (setq x (cons 'a 'b) y (list 1 2 3)) ⇒  (1 2 3) 
 (psetf (car x) 'x (cadr y) (car x) (cdr x) y) ⇒  NIL 
 x ⇒  (X 1 A 3) 
 y ⇒  (1 A 3) 

Backlinks

APPLY Forms as Places, apply, car, cdr, caar, cadr, cdar, cddr, caaar, caadr, cadar, caddr, cdaar, cdadr, cddar, cdddr, caaaar, caaadr, caadar, caaddr, cadaar, cadadr, caddar, cadddr, cdaaar, cdaadr, cdadar, cdaddr, cddaar, cddadr, cdddar, cddddr, case, ccase, ecase, compiler-macro-function, Constraints on the COMMON-LISP Package for Conforming Programs, Data and Control Flow Dictionary, defclass, define-condition, define-method-combination, define-setf-expander, define-symbol-macro, defsetf, defstruct, documentation, (setf documentation), Evaluation of Subforms to Places, fdefinition, find-class, first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth, flet, labels, macrolet, Function Call Forms as Places, Function Forms, get-setf-expansion, get, getf, gethash, Hash-Table Operations, hash-table-count, incf, decf, ldb, logical-pathname-translations, macro-function, macroexpand, macroexpand-1, make-load-form-saving-slots, maphash, mask-field, multiple-value-setq, nth, Other Compound Forms as Places, Overview of Places and Generalized Reference, remf, remove, remove-if, remove-if-not, delete, delete-if, delete-if-not, remove-duplicates, delete-duplicates, remprop, reverse, nreverse, rotatef, row-major-aref, Setf Expansions, setq, shiftf, slot-missing, slot-value, subseq, substitute, substitute-if, substitute-if-not, nsubstitute, nsubstitute-if, nsubstitute-if-not, Symbol Macros as Places, symbol-function, symbol-macrolet, symbol-plist, Symbols in the COMMON-LISP Package, THE Forms as Places, The Host part of a Logical Pathname Namestring, the, typecase, ctypecase, etypecase, values, with-accessors, with-slots