MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Set and Unevaluated

On 14 Aug., 12:26, Benjamin Hell <h... at> 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

/.{dummySet ->Set, dummyValue->computeMyValue[position], dummyPosition
-> position}

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.

  • Prev by Date: Re: Syntax coloring
  • Next by Date: Re: Straightforward factoring and simplification
  • Previous by thread: Re: Creating a simple state inspector for Mathematica
  • Next by thread: Snapping a Locator to an Interpolation curve