Originally mutable data structure has a getter AND a setter. Example for car
/rplaca
and cdr
/rplacd
:
CL-USER 68 > (let ((a (cons 1 2)))
(print (list (car a) (cdr a)))
(rplaca a 'foo)
(rplacd a 'bar)
(print (list (car a) (cdr a)))
(values))
(1 2)
(FOO BAR)
In this example the getter are car
and cdr
for cons cells.
The setters are rplaca
(replace car) and rplacd
(replace cdr).
Every mutable data structure has that and usually there is no systematic way to guess the name of the setter from knowing the name of the getter.
Thus the idea was to have a registry of getter and setter. Register a setter for a getter and the user has only to know the getter. The setf
macro (and others like incf
, decf
and also user defined macros) then does a lookup of the setter for the used getter.
The example above with the setf
macro looks like this:
CL-USER 69 > (let ((a (cons 1 2)))
(print (list (car a) (cdr a)))
(setf (car a) 'foo)
(setf (cdr a) 'bar)
(print (list (car a) (cdr a)))
(values))
(1 2)
(FOO BAR)
As you see the use of rplaca
and rplacd
has been replaced by the setf
macro.
Thus a place is basically a registered form, for which there is a setter. defsetf and define-setf-expander are used for that.
define-modify-macro is used to define a macro, which can modify a place.
For example we can define a way to multiply the value of a place, similar to incf
(increment a place) and decf
(decrement a place).
This feature is old and originally the word field was used instead of place. Thus the macros able to use a place end with f (field).
CL-USER 71 > (define-modify-macro multf (&rest args)
* "multiply")
MULTF
CL-USER 72 > (let ((a (cons 1 2)))
(print (list (car a) (cdr a)))
(multf (car a) 2)
(multf (cdr a) 4)
(print (list (car a) (cdr a)))
(values))
(1 2)
(2 8)