Re: Set and Unevaluated
- To: mathgroup at smc.vnet.net
- Subject: [mg111841] Re: Set and Unevaluated
- From: TJR <tilminator at googlemail.com>
- Date: Sun, 15 Aug 2010 07:36:24 -0400 (EDT)
- References: <i45r0l$fv2$1@smc.vnet.net>
On 14 Aug., 12:26, Benjamin Hell <h... at exoneon.de> wrote: > Hi, > this question is about the behavior of Set (represented by the > Operator), that I do not understand. The following simple example shows > the problem I have: > > Unevaluated[b] = 3 > Assigns the value 3 to the variable b. > > But the following does not work: > Evaluate[Unevaluated[b]] = 3 > This gives an set::write error: Tag Unevaluated is Protected. > > Obviously the Unevaluated is not removed because it is encapsulated by > Evaluate. But up to my understanding Evaluate should be applied before > the Set command is executed. > > The following shows an application I have in mind for using the above > syntax (if it would work the way I had expected it to). It is about > assigning multiple values to elements of a matrix by using a list for > the indexes of the elements. > > matrix = Table[0, {i, 1, 3}, {j, 1, 3}]; > list = {{1, 2}, {2, 3}}; > Evaluate[Unevaluated[matrix[[Sequence @@ #]]] & /@ list] = {1,2}; > > As already stated this does not work because the last line yields the > set::write error mentioned above. The Evaluate is needed above to apply > the Map (represented by /@) before the Set command is executed. > Otherwise the left hand side would not be Evaluated at all. > > So can anybody explain this behavior to me in detail? Why isn't > Unevaluated removed in Evaluate[Unevaluated[b]] = 3, when Evaluate > should have been executed before the Set command is executed? How can I > fix my example above? > > Any hint is much appreciated. Thanks in advance. I can't tell you why your approach doesn't work, but I can offer a workaround: HoldComplete[dummySet[matrixToAssign[[dummyPosition]],dummyValue] /.{dummySet ->Set, dummyValue->computeMyValue[position], dummyPosition -> position} //ReleaseHold; Use -> if you want the right hand side of the rules to be evaluated, e.g. iterating over position, and :> if you need it verbatim. Replacing Set / SetDelayed is generally a good habit because things like lhs[position][rhs_]:=dummyvalue introduce some wierd variable renaming to implement scoping.