|
|
|
|
|
by Blackthorn
1627 days ago
|
|
> That is false. No, it's not. Notice how, in my example, the resultant list is updated in the function parameter, but not the initial var defined by defvar. Whereas if I made an array via (make-array), passed it into the function, and updated that by the way the language documentation tells you to (setf and one of the aref functions), you'd end up with both the function parameter and initial var both pointing to the updated value. These are two logically different behaviors! And that's exactly what my criticism stated: "When you pass a value to a function, is it by value or by reference? Who knows? Rules are non obvious, do not follow the principle of least surprise." > Lists work this way because... It makes less sense to treat arrays that way. Yes, that's exactly the point. The language is inconsistent and does not follow the principle of least surprise. |
|
Value. Value, value, always value. However, it seems that maybe you don't understand exactly what the value is that you're passing.
> Notice how, in my example, the resultant list is updated in the function parameter
No [0]. It makes a new list (really a new cons cell, which contains the new item and then points to the old list [1]), and assigns that to v. And your example doesn't actually show this update happening, it just shows the return value of push (it so happens that it is updated, however). The original list --- passed in or otherwise --- is never changed. You say that you're "updating" a list, but you're not mutating or updating your data structure at all --- you're making something new, and assigning that to v.
> These are two logically different behaviors!
They are two logically different operations. Are you sure that you understand the data structures you're working with, and the operators that you're calling on them? Did you perhaps try to map concepts from another language into Common Lisp, find functionality that looked similar on the surface, then become surprised when the results were not identical?
Linked lists differ fundamentally in structure from arrays, and so the operators which are commonly used with them differ in turn. Perhaps you would like to compare arrays to vectors, as a closer approximation in data structures, with similar typical strategies of manipulation?
> does not follow the principle of least surprise.
You keep saying this, as if that is that. But nothing about your example is surprising to me, so I suppose this is a matter of perspective.
[0] http://clhs.lisp.se/Body/m_push.htm
[1] https://en.wikipedia.org/wiki/Linked_list