Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*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 2004

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

Search the Archive

RE: ReplaceList -- Unexpected Answer

  • To: mathgroup at smc.vnet.net
  • Subject: [mg46808] RE: [mg46788] ReplaceList -- Unexpected Answer
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Tue, 9 Mar 2004 04:30:51 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

>-----Original Message-----
>From: Harold.Noffke at wpafb.af.mil [mailto:Harold.Noffke at wpafb.af.mil]
To: mathgroup at smc.vnet.net
>Sent: Monday, March 08, 2004 10:10 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg46808] [mg46788] ReplaceList -- Unexpected Answer
>
>
>MathGroup:
>
>In The Mathematica 5 Book, Section 2.3.3 Naming Pieces of Patterns, we
>find the following pattern matching exercise ...
>
>	Now both arguments of f are constrained to be the same, 
>and only the
>	first case matches.  
>
>	In[5]:= 
>		{f[h[4], h[4]], f[h[4], h[5]]} /. f[x:h[_], x_] -> r[x]
>	Out[5]= 
>		{r[h[4]],f[h[4],h[5]]}
>
>
>Now, let's use ReplaceList to get more insight into this matching
>process ...
>
>	In[6]:=
>		ReplaceList[ 
>{f[h[4],h[4]],f[h[4],h[5]]},f[x:h[_],x_] -> r[x] ]
>	Out[6]=
>		{}
>		
>I do not understand why ReplaceList returns {} instead of { r[h[4]] }.
>
>Regards,
>Harold
>

Harold,

from Help:
"ReplaceList[expr, rules] attempts to transform the entire expression expr
by applying a rule or list of rules in all possible ways, and returns a list
of the results obtained."

That is: the pattern from the rules have to match the entire expression.
Such try

In[2]:= ReplaceList[{f[h[4], h[4]], 
    f[h[4], h[5]]}, {a___, f[x : h[_], x_], o___} -> {a, r[x], o}]
Out[2]=
{{r[h[4]], f[h[4], h[5]]}}

You get only one match!


This gives two matches:

In[3]:= ReplaceList[{f[h[4], h[4]], 
    f[h[5], h[5]]}, {a___, f[x : h[_], x_], o___} -> {a, r[x], o}]
Out[3]=
{{r[h[4]], f[h[5], h[5]]}, {f[h[4], h[4]], r[h[5]]}}


--
Hartmut


  • Prev by Date: Re: ReplaceList -- Unexpected Answer
  • Next by Date: Accounting of downloads
  • Previous by thread: RE: ReplaceList -- Unexpected Answer
  • Next by thread: Re: Shared variables between notebooks?