LISP - destructive and non-destructive structures

What is the correct definition of destructive and non - destructive constructions in LISP (or in general). I tried to find the actual meaning, but I found a lot of use of these terms without explaining them.

It seemed to me that a destructive function is a function that changes the meaning of a construct (or variable) - therefore, when I pass a list as a parameter to a function that changes it, it is called a destructive operation, because it changes the original list and returns a new one. Is this correct or are there some exceptions?

So, for example, a destructive function is installed (because it changes the value of x)? I think not, but I don’t understand how I would justify it.

(set 'x 1) 

Sorry, probably a very simple question .... Thanks for any answers!

+7
source share
2 answers

I would not interpret the word "destructive" too much.

In list processing, a destructive operation is one that potentially changes one or more input lists as a visible side effect.

Now you can expand the value of operations on arrays, structures, CLOS objects, etc. You can also call the assignment of the variable "destructive", etc.

In Common Lisp, it makes sense to talk about destructive operations on sequences (which are lists, strings and vectors in general) and multidimensional arrays.

+6
source

Practical general Lisp distinguishes two types of destructive operations: operations with a side effect and operations on recycling.

set is a destructive and side effect: it always changes its first argument. Remember that it changes the binding for a symbol, but not the thing attached to that symbol. setf can change bindings or objects in place.

In contrast, nreverse recycled: it is allowed to change the list of its arguments, although there is no guarantee that it will be so, therefore it should be used in the same way as reverse (take the return value), except that the input argument can be " destroyed "and should no longer be used. [Schema programmers may call this the "linear update" function.]

+5
source

All Articles