Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1998
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1998

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

Search the Archive

Re: Is this a bug?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg13439] Re: Is this a bug?
  • From: "Allan Hayes" <hay at haystack.demon.cc.uk>
  • Date: Fri, 24 Jul 1998 01:45:29 -0400
  • References: <6p6oa9$56m@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

jfreeze at lexmark.com wrote in message <6p6oa9$56m at smc.vnet.net>...
>
>The following seems to be a bug. In the first example, both the true and
>the false parts of If are being evaluated. In the secon example, only
>the true part. If this is not a bug, could someone please explain this
>behavior to me. Thanks
>
>In[1]:=
>     Clear[x,y];
>
>If[True,Evaluate[ToExpression["x=5"]],Evaluate[ToExpression["y=5"]]];
>     Print[x,y];
>Out[1]:=
>     55
>In[2]:=
>     Clear[x,y];
>     If[True,x=5,y=5];
>     Print[x,y];
>Out[2]:=
>     5y
>

This is not a bug.
To try and explain the evaluation I will modify your example slightly to

(1)
In[1]:=
    Clear[x,y];

In[2]:=
    If[True,x=5,y=7]
Out[2]=
    5

In[3]:=
    {x,y}
Out[3]=
    {5,y}

(2)
In[4]:=
    Clear[x,y];

In[5]:=
    If[True,Evaluate[x=5],Evaluate[y=7]] Out[5]=
    5

In[6]:=
    {x,y}
Out[6]=
    {5,7}

Notice that the If has the attrribute HoldRest

Here is what happens

(1) For If[True,x=5,y=7]
The *attribute* Hold Rest for If prevents evaluation of x=5 and x=7; so
we get
 If[True,x=5,y=7]
The *rules* for If are now used and give
  x=5
This is evaluated to ouput 5 and the rule x = 5 is stored Because of the
stored rule {x,y} gives {5, y}

(2) For If[True,Evaluate[x=5],Evaluate[x=7]] The *attribute* Hold Rest
is overidden at  Evaluate[x=5] which evaluates to 5 and stores x =5;
similarly for Evaluate[y=7]; so we get
 If[True, 5 , 7 ] and the rules x = 5 and y = 7 have been stored The
*rules* for If are now used and give
 5
Because of the stored rule {x,y} gives {5, 7}

You can see thes stages displayed by using TracePrint


Clear[x,y];
If[True,x=5,y=7]//TracePrint


Clear[x,y];
If[True,Evaluate[x=5],Evaluate[y=7]]//TracePrint


------------------------------------------------------------- 
Allan Hayes
Training and Consulting
Leicester UK
http://www.haystack.demon.co.uk
hay at haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44(0)116 271 8642




  • Prev by Date: Re: Install[] and command line args?
  • Next by Date: Re: Is this a bug?
  • Previous by thread: Re: Re: Is this a bug?
  • Next by thread: Re: Is this a bug?