What is the correct definition of destructive and non-destructive constructs in LISP (or in general). I have tried to search for the actual meaning but I have only found a lot of usage of these terms without actually explaining them.
It came to my understanding, that by destructive function is meant a function, that changes the meaning of the construct (or variable) - so when I pass a list as a parameter to a function, which changes it, it is called a destructive operation, because it changes the initial list and return a brand new one. Is this right or are there some exceptions?
So is for example set a destructive function (because it changes the value of x)? I think not but I do not how, how would I justify this.
(set 'x 1)
Sorry for probably a very basic question.... Thanks for any answers!
Practical Common Lisp distinguishes two kinds of destructive operations: for-side-effect operations and recycling operations.
set
is destructive and for-side-effect: it always modifies its first argument. Beware, that it changes the binding for a symbol, but not the thing currently bound to that symbol.setf
can change either bindings or objects in-place.By contrast,
nreverse
is recycling: it is allowed to modify its argument list, although there's no guarantee that it will, so it should be used just likereverse
(take the return value), except that the input argument may be "destroyed" and should no longer be used. [Scheme programmers may call this a "linear update" function.]I would not interpret too much into the word 'destructive'.
In list processing a destructive operation is one which potentially changes one or more of the input lists as a visible side effect.
Now you can widen the meaning to operations over arrays, structures, CLOS objects, etc. You can also call variable assignment 'destructive' and so on.
In Common Lisp it make sense to talk about destructive operations over sequences (which are lists, strings, and vectors in general) and multi-dimensional arrays.