Consider the cons x1
:
(setq x1 '(a . (b c))) => (a b c)
or in list notation:
(setq x1 '(a b c)) => (a b c)
and the cons x2
, built on x1
:
(setq x2 (cons 'A (cdr x1))) => (A b c)
cons
help (in Emacs) says that the function creates a new cons, gives it the arguments, 'A
and (cdr x1)
, as components and returns it. There is nothing in it suggesting that the life of the newly returned cons will be linked to that of its generating components.
Anyway if one modifies the copy, x2
, also the originating cons, (a . (b c))
gets modified:
(setcar (cdr x2) 'B) => B
x2 => (A B c) ; as from the assignment
x1 => (a B c) ; x1 and x2 are linked
Other function examples can show the link between x1
and x2
.
(setq x1 '(a . (b c))) => (a b c)
(setq x2 (cons 'A (cdr x1))) => (A b c)
(nreverse x2) => (c b A)
x1 => (a b A)
I took this example from the documentation of setcar
in the Emacs Lisp Reference Manual, which states that the "cons cell is part of the shared structure" and the cdr of x1
and x2
is referred to as a "shared link" and x1
, x2
are graphically shown like (slightly adapted):
x1:
-------------- -------------- --------------
| car | cdr | | car | cdr | | car | cdr |
| a | o------->| b | o------->| c | nil |
| | | -->| | | | | |
-------------- | -------------- --------------
|
x2: |
-------------- |
| car | cdr | |
| A | o----
| | |
--------------
This is something reminiscent of C pointers, in that the cdr of x2
is not a copy, but "points" to the cdr of x1
. Clear, but I wonder when this situation practically arise, that is, how can I know whether (an element of) a cons points to another one or is a self living copy? More generally what (where) is a formal definition of shared structure and shared links?
In the Emacs Lisp Reference Manual there is no explicit mention of them. In fact a search for "shared" or "link" in its index returns (excluding file/web links) only the indirect reference to "shared structure, read syntax", dealing with their representation, not on what they are.
Curiously searching the PDF for "shared" lands, as first occurrence, to the section "Read Syntax for Circular Objects", starting with "To represent shared or circular structures...". Unfortunately there is no prior mention of the words shared and circular (structures)! The next occurrence is the mentioned setcar
documentation.
So it seems that there are implicit pointers in Lisp/Elisp, but no one is willing to tell about them.))