|
|
|
|
|
by Blackthorn
1621 days ago
|
|
No, there really is no consistency to Common Lisp's mutation. $ sbcl
* (defvar a (list))
A
* a
NIL
* (defun x (v) (push 'b v) v)
X
* (x a)
(B)
* a
NIL
Now if you were to do similar with something like an array, the original variable would be mutated. Just another example of how Common Lisp doesn't have any sort of internal consistency. Once again, rules are non obvious, do not follow the principle of least surprise. |
|
That is false. To do a similar thing with an array, we need a non-mutating operation which returns a new array which is like an existing array, but with an element prepended.
Then we need a macro to mutate a place to replace an existing array in that place with a new such an array.
Then, exactly the same kind of behavior will be reproduced:
What? Of course; we are not mutating any object here, but a variable: the local variable of x. Lists work this way because they are made of cells, and those cells are immutable (if you want them to be) for very good reasons. This is part of the essence of Lisp since the dawn of the language.It makes less sense to treat arrays that way. It's possible, but you need an exotic data structure to do it even halfway efficiently; that structure will never be as efficient as an ordinary mutable array for ordinary array work.
Whereas, treating singly linked lists this way is almost free of additional cost.