Home

setf

Setf is a macro in Common Lisp that generalizes assignment to a destination, or place. It is not a function; its purpose is to store a value in a place that can be a simple variable or a more complex location such as an array element, a slot in a structure, or a user-defined location expanded by a setf mechanic. The setf macro takes a place designator and a value expression, evaluating the value and storing it in the specified place.

A place, in this context, is any location capable of holding a value. Simple examples include a

In Common Lisp, (setf place new-value) evaluates new-value and stores it in place, returning the stored value.

Related concepts include setq for variable assignment and the general notion of places in the language. Setf

See also: defsetf, define-modify-macro, setq.

variable
(setq
is
used
in
conjunction
with
setf
for
variables)
and
an
element
of
an
array
or
list,
such
as
(setf
(aref
arr
i)
42)
or
(setf
(nth
2
list)
'x).
More
complex
places
include
structure
slots
(setf
(slot-name
obj)
value)
and
results
of
functions
that
are
defined
to
act
as
setf
places,
via
setf
expanders.
The
setf
form
thus
enables
a
uniform
syntax
for
a
wide
range
of
assignment
targets.
The
mechanism
can
be
customized
for
user
types
using
defsetf
and
define-modify-macro,
allowing
new
kinds
of
places
to
participate
in
setf
semantics
without
changing
their
call
sites.
is
a
core
tool
for
writing
generic,
flexible
assignment
code
in
Lisp
programs.