Re: replace by value in expr
- To: mathgroup at yoda.ncsa.uiuc.edu
- Subject: Re: replace by value in expr
- From: CAMERON at midd.cc.middlebury.edu
- Date: Wed, 19 Dec 90 08:12:34 -0600
In a response to Charles Herrick about the workings of "/." (ReplaceAll) and "->" (Rule), David Jacobson (jacobson at cello.hpl.hp.com) writes: > ... If you want the x on the left of "->" to not be > replaced by the value of x *before substition* use ":>" instead. BUZZZ! Sorry, no, but thank you for playing... :-) Using ":>" (RuleDelayed) instead of Rule defers evaluation of the right-hand side of the rule only. (This is because RuleDelayed has the "HoldRest" attribute rather than the "HoldAll" attribute.) If you want to defer evaluation on the left (with either Rule or RuleDelayed), use Literal. The following examples show the four possibilities (hold neither, hold left, hold right, and hold both): In[6]:= x = 5 Out[6]= 5 In[7]:= x -> 1 + 1 Out[7]= 5 -> 2 In[8]:= x :> 1 + 1 Out[8]= 5 :> 1 + 1 In[9]:= Literal[x] -> 1 + 1 Out[9]= Literal[x] -> 2 In[10]:= Literal[x] :> 1 + 1 Out[10]= Literal[x] :> 1 + 1 In[11]:= Attributes[RuleDelayed] Out[11]= {HoldRest, Protected} You could use Hold instead of Literal to defer evaluation in the LHS of rules, but Literal is transparent to the pattern matcher whereas Hold is not (i.e. "Literal[x]" will match in "x + 4" but "Hold[x]" would not, because the matcher would look for the symbol "Hold"). Perhaps WRI could have come up with a more symmetric way of holding in rules (say, operators ->-, :>-, ->:, and :>: for holding nothing, left, right, and both), but in practice it is FAR more common to want to hold the right than the left side of a rule. Hope this helps. --Cameron Smith Mathematica Consultant CAMERON at MIDD.BITNET --or-- cameron at midd.cc.middlebury.edu