Re: Question on Unevaluated
- To: mathgroup at smc.vnet.net
- Subject: [mg117298] Re: Question on Unevaluated
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Mon, 14 Mar 2011 06:00:33 -0500 (EST)
Alexey, You forgot about the CompoundExpression (;). You only attempted to prevent the evaluation of 1+1 inside (1+1;3), but not the total result for CompoundExpression, which is the value of the last statement (2+1 in this case). This is what you probably had in mind: In[9]:= f[Unevaluated[(1 + 1; 2 + 1)]] Out[9]= f[Unevaluated[1 + 1; 2 + 1]] What is perhaps less obvious is that you did not prevent the evaluation of 1+1 either. Here is a simple way to check it: In[14]:= f[Unevaluated[Print["*"]]; 2 + 1] During evaluation of In[14]:= * Out[14]= f[3] The problem is that Unevaluated is only effective once. To totally prevent something from evaluation, you have to know the exact number of sub-evaluations (which is generally impossible to know since it can be data-dependent), and wrap in as many levels of Unevaluated. In this case, the following will do: In[13]:= f[Unevaluated[Unevaluated[Print["*"]]]; 2 + 1] Out[13]= f[3] But as I said, this is not a robust approach, and in such cases you will be better to use Hold or similar for a persistent holding wrapper, stripping it off later when needed. You may want to check out e.g. this thread http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/bfd67e9122b1fdec (my second post there), where I elaborate on these issues. HTH. Regards, Leonid On Sun, Mar 13, 2011 at 1:26 PM, Alexey <lehin.p at gmail.com> wrote: > Hello, > > I am puzzled a bit by the Documentation for Unevaluated. Under "More > information" field we read: > > "f[Unevaluated[expr]] effectively works by temporarily setting > attributes so that f holds its argument unevaluated, then evaluating > f[expr].". > > After reading this I expect that > > f[Unevaluated[1 + 1]; 2 + 1] > > will be returned completely unevaluated as it is when I set HoldFirst > attribute to f: > > In[2]:= SetAttributes[f, HoldFirst] > f[Unevaluated[1 + 1]; 2 + 1] > > Out[3]= f[Unevaluated[1 + 1]; 2 + 1] > > But in really we get > > In[1]:= f[Unevaluated[1 + 1]; 2 + 1] > > Out[1]= f[3] > > This leads me to a question: what is implied in documentation? Which > attributes are temporarily set and to which function? > >