[Date Index]
[Thread Index]
[Author Index]
Re: list mutability (very basic question)
*To*: mathgroup at smc.vnet.net
*Subject*: [mg131712] Re: list mutability (very basic question)
*From*: Itai Seggev <itais at wolfram.com>
*Date*: Mon, 23 Sep 2013 21:57:09 -0400 (EDT)
*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com
*Delivered-to*: l-mathgroup@wolfram.com
*Delivered-to*: mathgroup-outx@smc.vnet.net
*Delivered-to*: mathgroup-newsendx@smc.vnet.net
*References*: <20130921083830.751226A65@smc.vnet.net>
> I just realized that I am unclear on a very basic point: in what ways are
> lists mutable?
>
> Example:
> {0}[[1]]=1; (* a. this is an error *)
> x={0}
> x[[1]]=1 (* b. this is not an error *)
> ReplacePart[x, 1 -> 2] (* c. this is differently not an error *)
>
> http://reference.wolfram.com/mathematica/tutorial/ManipulatingListsByTheirI
>ndices.html It is important to understand that ReplacePart always creates a
> new list. It does not modify a list that has already been assigned to a
> symbol the way does.
There two levels here; what I'll call user-mutability and internal-mutability.
On a user level, Set modifies the (definition of) the variables it acts upon,
so after it has evaluated the variable will have a new value, and it returns
the new value of the variable. ReplacePart does not; it will first make a
copy, modify it, and then return that new expression. I think this is
relatively clear
The there's internal mutability, whether and when expressions can be shared and
mofied in place. This depends very sensitivily on your state. For example, if
a = {1,0}
b = a
b[[1]]= 2
Then a and b can point the same list after the 2nd input is evaluated, but not
after the third. So in this case it would likely generate a copy and b would
be updated to point to the new location. For a user perspective how, it is
still "b that was modified".
> I think this means that after b. (i.e., Set[Part[x,1],1]) that x still
> references the same location in memory. If so, why is a. an error? (I
> realize that someone will think that saying that {0} is not a L-value will
> be a helpful answer, and perhaps it should be, but I'm looking for a
> different angle on the answer.)
I'm confused by this comment. Do you think that 0=1 should not be an error? If
you think it should be an error (as it is in every language I'm familiar with),
then why should {0}[[1]]=1, which would essentially be the same thing, not be
an error?
--
Itai
Itai Seggev
Software Technology
Prev by Date:
**Re: Quantity**
Next by Date:
**Re: Import CSV can not understand string correctly**
Previous by thread:
**Re: list mutability (very basic question)**
Next by thread:
**Re: list mutability (very basic question)**
| |