Re: list mutability (very basic question)
- To: mathgroup at smc.vnet.net
- Subject: [mg131724] Re: list mutability (very basic question)
- From: Itai Seggev <itais at wolfram.com>
- Date: Tue, 24 Sep 2013 04:15:54 -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>
On Sun, Sep 22, 2013 at 10:27:29AM -0400, Alan G Isaac wrote: > On 9/22/2013 3:28 AM, Itai Seggev wrote:> 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? > > > Because I do not expect this syntax to lead to the LHS to be evaluated to 0. > I expect it to mean: replace the first element of the list {0} with the value 1. > (Compare Python: ``[0][0]=1`` is not an error, for exactly this reason.) You should not have the latter expectation. From the documentation lhs=rhs evaluates rhs and assigns the result to be the value of lhs. From then on, lhs is replaced by rhs whenever it appears. If Set did what you propose, then {0} would evaluate {1}, which I think we all agree would be very confusing. > In fact this is a key point of puzzlement for me: > why ``x={0};x[[1]]=1`` is ok but ``{0}[[1]]=1`` is an error in Mathematica. > What is the evaluation rule for the LHS that produces this difference? Set and related functions are assignment operators. They do not directly create new expressions, although clearly this may happen as part of the assignment. What they do is modify the definitions attached to symbols (OwnValues, DownValues, UpValues, SubValues, and FormatValues). Moreover, they have the attribute HoldFirst. When Set begins evaluating, the first thing it does is search for the symbol whose definition will be modified and what type of definition will change (which for Set could be an OwnValue, DownValue, or FormatValue). If there is no symbol to attach the definition to, it will throw an error. In your example, Set sees the expression x[[1]]; it determines that the x is the symbol and that it will be changing its first element of its OwnValue; it makes sure that x is non-atomic and non-empty; and finally it does the equivalent of x = ReplacePart[x, 1-> 1] (so x[[1]] = 1 is really just syntacic sugar for this last expression). The expression {0} doesn't appear until that last step, on then only effectively of the RHS of the Set. So it really all boils down to the lack of lvalue, which is not what you wanted to here, but perhaps my explanation at least clears up the evaluation issue. To be totally honest, the fact that Python allows an assignment operator without an lvalue / as a replacement operator is quite wierd to my miind, whether thinking as an M- programmer, a C programmer, or a shell scripter. But I'm not a Python programmer, so perhaps this makes sense in their mileu. -- Itai Seggev Mathematica Algorithms R&D
- References:
- list mutability (very basic question)
- From: Alan <alan.isaac@gmail.com>
- list mutability (very basic question)