The original Pair.
(define x '("The " "Rain " "in " "Spain") )
|
Returns: #<Pair 123456>
|
The copied Pair using the copy function.
(setq z (copy x))
|
Returns: #<Pair 234567>
|
The copied Pair using the setq function.
(setq twin x)
|
Returns: #<Pair 345678>
|
After the first setq function, the values of Pair z are:
After the first setq function, the values of Pair x are:
The container named z is a copy of x (both are Pairs) and z has a separate
memory space. However, the containers named x and twin point to the same memory
spaces on the heap. Therefore
(setq twin[1] "Hail" )
|
Returns: #<Pair 123456>
|
The setq command results in the contents of both twin and x being both the same.
Since z points to a different memory space, it still retained the original value
which was copied from x before the values of x were modified.
Notes and Hints
Using the setq function results in objects having the same object id.
But the object that is being assigned the copied object has a
different object id than original object. In this example twin and
x have the same object id while z has a different object id.