Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SETFable vs place (CLHS) vs location (Norvig)

Is a setfable the same as a place in CLHS and a location in Norvig's PAIP?

I'm trying to figure out what exactly a place is in Common Lisp but to me the HyperSpec's explanation

place n. 1. a form which is suitable for use as a generalized reference. 2. the conceptual location referred to by such a place[1].

is only of limited help.

(I know it's not really the kind of question that suits SO but if someone knows a good article that explains setfable/place/location I'd appreciate a link/reference)

like image 328
Frank Avatar asked Apr 16 '17 15:04

Frank


1 Answers

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) 
like image 91
Rainer Joswig Avatar answered Oct 26 '22 14:10

Rainer Joswig