Hi marmorine,

> and there I was stumped at first, wondering what is
> destructive, what is not and so forth (or what that even IS)

You are right. It is a confusing issue. Let me try to explain it a
little.

An operation is destructive when it modifies a data structure in such a
way that other places in the program, which refer to that structure, can
see the modifications.

Set the value of a symbol 'A' to a list:

   : (setq A '(a b c d))
   -> (a b c d)

then set another symbol 'B' to the same list:

   : (setq B A)
   -> (a b c d)


Now we can NON-destructively exchange the first two elements of the list
(the CAR and the CADR) in A:

   : (setq A (cons (cadr A) (car A) (cddr A)))
   -> (b a c d)

Now 'A' contains (b a c d), while 'B' still points to the old list:

   : B
   -> (a b c d)


Now let's do the same destructively:

   : (setq A '(a b c d))
   -> (a b c d)

   : (setq B A)
   -> (a b c d)

Both 'A' and 'B' point to the same list again.

A destructive modification of the list could be done as:

   : (xchg (cdr A) A)
   -> b

As we can see, the first two elements are exchanged, as before:

   : A
   -> (b a c d)

But now the value of 'B' has changed too:

   : B
   -> (b a c d)

This is, because the list (both pointed to by 'A' and 'B') was modified
destructively. This may be desired or not, depending on the situation.

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

Reply via email to