# Re: altering lists (TUI program launcher)

```On Sun, Jan 18, 2015 at 11:36:25PM +0100, steven wrote:
> That rot or xchg are destructive is fine, also the difference between
> that and non-destructive. But I am wondering about how the set and nth
> work together to force an operation to be destructive, like here:```
```
No. 'set' is always destructive, because it modifies a 'var' (the value
of a symbol, or the CAR of a cell).

It helps a lot if you visualize the situation with box diagrams.

> : (set 'A '(a b c))
> -> (a b c)

This means that the value of the symbol 'A' points to a list (a b c):

+-----+-----+
|  A  |  |  |
+-----+--+--+
|
V
+-----+-----+     +-----+-----+     +-----+-----+
|  a  |  ---+---> |  b  |  ---+---> |  c  |  /  |
+-----+-----+     +-----+-----+     +-----+-----+

> : (set 'B A)
> -> (a b c)

Now you set another symbol 'B' to the same value. We have the situation
that both the symbol 'A' and the symbol 'B' point to the same list (a b
c):

+-----+-----+
|  A  |  |  |
+-----+--+--+
|
V
+-----+-----+     +-----+-----+     +-----+-----+
|  a  |  ---+---> |  b  |  ---+---> |  c  |  /  |
+-----+-----+     +-----+-----+     +-----+-----+
^
|
+-----+--+--+
|  B  |  |  |
+-----+-----+

> : (set (nth A 1) 'x)
> -> x

Note that (nth A 1) is a no-op. 'A' points to the first cell of a list,
and with 'nth' you still point to the first cell. You could write

: (set A 'x)

instead. In any case, it replaces the 'a' in (a b c) with 'x'. We get:

+-----+-----+
|  A  |  |  |
+-----+--+--+
|
V
+-----+-----+     +-----+-----+     +-----+-----+
|  x  |  ---+---> |  b  |  ---+---> |  c  |  /  |
+-----+-----+     +-----+-----+     +-----+-----+
^
|
+-----+--+--+
|  B  |  |  |
+-----+-----+

> : A
> -> (x b c)
> : B
> -> (x b c)

As both 'A' and 'B' point to that cell, we see (x b c) in both cases.

The CAR of the first cell was "destroyed".

> B points at the original value:
>
> : (set 'A '(a b c d))
> -> (a b c d)
> : B
> -> (x b c)

Right. After setting 'A' to a new value, we have:

+-----+-----+
|  A  |  |  |
+-----+--+--+
|
V
+-----+-----+     +-----+-----+     +-----+-----+     +-----+-----+
|  a  |  ---+---> |  b  |  ---+---> |  c  |  ---+---> |  d  |  /  |
+-----+-----+     +-----+-----+     +-----+-----+     +-----+-----+

+-----+-----+     +-----+-----+     +-----+-----+
|  x  |  ---+---> |  b  |  ---+---> |  c  |  /  |
+-----+-----+     +-----+-----+     +-----+-----+
^
|
+-----+--+--+
|  B  |  |  |
+-----+-----+

Does this help?

It is essential to understand what happens on this level. That's why the
initial part of the PicoLisp reference dwells so much on this, in "The
PicoLisp Machine" in http://software-lab.de/doc/ref.html#vm

♪♫ Alex
--
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
```