Re: Error in Mathematica?
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: Error in Mathematica?
- From: jacobson at cello.hpl.hp.com
- Date: Mon, 08 Nov 93 08:22:53 -0800
J Harris observes that
In[1]:= x=b
Out[1]= b
In[2]:= x^2
Out[2]= b^2 (* as I would expect)
In[3]:= x^2/.x->a
Out[3]= a^2
In[4]:= x=2
Out[4]= 2
In[5]:= x^2/.x->7
Out[5]= 4
What is happening is that the left side of the Rule operator (->)
gets evaluated, so by the time Rule gets to work on it here is what it
is seeing
In[1]:= x=b
Out[1]= b
In[2]:= x^2
Out[2]= b^2 (* as I would expect)
In[3]:= x^2/.x->a b^2 /. b->a
Out[3]= a^2
In[4]:= x=2
Out[4]= 2
In[5]:= x^2/.x->7 4 /. 2->7
Out[5]= 4
In item 3, the b gets replaced by a. But in item 5, 2 does not match
4, so no replacement is done.
You can get the behavior you expect in item 3 by using Literal, as
follows
In[1]:= x=b
Out[1]= b
In[2]:= x^2 /. Literal[x]->a
2
Out[2]= b
-- David Jacobson