MathGroup Archive 2001

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

Search the Archive

Re: Replacement Rule


Brian Higgins wrote:
> 
> I have the following list
> 
> l1={{1,2},{3,4},{5,6}};
> 
> and then I operate on it with the following pattern/replacement rule
> 
> In[3]:= l1 /. {x_, y_} -> x
> 
> Out[3]= {1, 3, 5}
> 
> This is what I would expect. Now consider the following list
> 
> In[4]:= l2 = {{3, 4}, {5, 6}};
> 
> Then I use the same pattern/replacement rule
> 
> In[5]:=l2 /. {x_, y_} -> x
> 
> Out[5]= {3, 4}
> 
> I was hoping to get
> 
> {3,5}
> 
> What am I missing? The FullForm of l2 is basically the same structure
> as l1, as far as I can tell....
> 
> Brian

Replace[l2, rule, {1}]
will give the result you expected. More details in the help on "Replace", along
with Chapter A 3.6 of the Book for the levels. Be aware that "/." does a
"ReplaceAll". It recognizes the pattern given in the rule already at the
outermost level of l2, which is of course a list of two elements itself
(ignoring that these elements again are lists). I guess this sort of explains
it.

Ciao
   -erk-
-- 
 Dr.-Ing. Erk JENSEN                    mailto:Erk.Jensen at cern.ch
 CERN  PS/RF  L19510                    http://cern.ch/Erk.Jensen
 CH-1211 Geneva 23                      Tel.:     +41 22 76 74298
 Switzerland                            Fax.:     +41 22 76 78510


  • Prev by Date: RE: Replacement Rule
  • Next by Date: Re: Solutions that are not solutions
  • Previous by thread: RE: Replacement Rule
  • Next by thread: RE: Replacement Rule