MathGroup Archive 2003

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

Search the Archive

RE: Unevaluated

  • To: mathgroup at
  • Subject: [mg44485] RE: [mg44465] Unevaluated
  • From: "Wolf, Hartmut" <Hartmut.Wolf at>
  • Date: Wed, 12 Nov 2003 08:01:34 -0500 (EST)
  • Sender: owner-wri-mathgroup at

>-----Original Message-----
>From: Hans-Peter Kunzle [mailto:hp.kunzle at]
To: mathgroup at
>Sent: Tuesday, November 11, 2003 1:56 AM
>To: mathgroup at
>Subject: [mg44485] [mg44465] Unevaluated
>I was trying to make a function like 'tPrint[x___]' that would act
>like Print if some global variable Testing was True and do nothing
>if Testing was False. This would allow me to simply add or delete
>the 't' to individually turn on or off some tracing. It would
>be easier than to comment or uncomment the Print statements each
>But when I tried
>	tPrint[Unevaluated[x___]]:= If[Testing,Print[x]]
>I found, using TracePrint, that 'x' gets evaluated each time whether
>or not Testing is True.
>Maybe I do not understand properly how Unevaluated operates.
>Is there a way to do what I want?
>Any help is appreciated.

Let's see:
In[2]:= Testing = False; a = 1; b := (Print["oups"]; 2)

In[3]:= tPrint[Unevaluated[x___]] := If[Testing, Print[x]]

In[4]:= tPrint[a, "; ", b]
>From In[3]:= "oups"

Obviously b got evaluated, although Print[x] wasn't executed. Now, what
happend to that Unevaluated?

In[5]:= ?tPrint
   tPrint[x___] := If[Testing, Print[x]]

It disappeared from the definition (this is because at definition time the
lhs already is being partially evaluated), but anyway, it's not the right

Instead, you may suppress evaluation at execution time wrapping the
arguments with Unevaluated:

In[6]:= tPrint[Unevaluated[a, "; ", b]]

(no output, i.e. Null, the result from If[False,...])

This is a bit nasty, however. To simply achieve what you want, just give
tPrint the Hold attribute:

In[7]:= Attributes[tPrint] = HoldAll;

In[8]:= tPrint[x___] := If[Testing, Print[x]]

(this only for clarity, your tPrint already is this), 

In[9]:= tPrint[a, b]

(no output)

In[10]:= Testing = True;
   tPrint[a, "; ", b]
>From In[10]:=
>From In[10]:=
   1; 2

It might be amusing to ask, how to get that Unevaluated into the definition.

In[12]:= txPrint[Unevaluated[Unevaluated[x___]]] := If[Testing, Print[x]]

In[13]:= ?txPrint
   txPrint[Unevaluated[x___]] := If[Testing, Print[x]]

(one Unevaluated got stripped), now:

In[14]:= txPrint[a, "; ", b]
>From In[14]:=
txPrint[1, "; ", 2]

b is executed, but then there was no transformation rule for resulting 
argument sequence 1,"; ",2 (as Unevaluated is missing from the pattern).

In[15]:= txPrint[Unevaluated[a, "; ", b]]
Out[15]= txPrint[Unevaluated[a, "; ", b]]

Unevaluated gets stripped (and its arguments not evaluated), resulting in 
argument sequence a,"; ",b  but this again does not match (as Unevaluated is

In[16]:= txPrint[Unevaluated[Unevaluated[a, "; ", b]]]
>From In[16]:=
>From In[16]:=
   1; 2

Here the pattern does match (after stripping off the outer Uevaluated.


  • Prev by Date: Re: Just trying to import an image
  • Next by Date: Re: NIntegrate singularity problem v4.0 vs v5.0
  • Previous by thread: Re: Unevaluated
  • Next by thread: Re: Unevaluated