[Warning : these are noob questions.]
I'm a beginner in Emacs Lisp and I would like to be sure that I understand well what I'm really doing when I set a value to a variable with setq
or let
.
Here is a piece of code:
(setq x '(1 2 3 4)) ; define x
(setq y x) ; define y
(setcar y 9) ; modify CAR of y
y ; -> (9 2 3 4): y has changed (ok)
x ; -> (9 2 3 4): but x has changed too!
It seems that when you define a symbol and give it the value of another symbol, this basically means that the two symbols become the same object?
(eq x y) ; -> t
(I expected that the instruction
(setq y x)
would make an "independant copy" ofx
, as it would be the case if you doy <- x
in R language for example. Or, more formally, I thought this instruction would only fill the "value cell" ofy
by evaluating(symbol-value 'x)
, but without "binding" those two objects together.)This is really a matter of pointers, if I understand well.
(setq y x)
creates a new symbol which is basically bound to the same address asx
? (I.e.,y
points towardsx
which points towards a given value, and so if you modifyy
, you will also modifyx
because both of them point towards the same address "by transitivity"?)Robert Chassell's book says that "when a Lisp variable is set to a value, it is provided with the address of the list to which the variable refers", but I cannot figure out what this means formally (where is this address stored?). A Lisp symbol is made of 4 components (name, value, function, properties). So, when I do
(setq y x)
, the "value cell" ofy
is really an address / a pointer towardsx
?