Services & Resources / Wolfram Forums
MathGroup Archive
*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
  • Subject: [mg46808] RE: [mg46788] ReplaceList -- Unexpected Answer
  • From: "Wolf, Hartmut" <Hartmut.Wolf at>
  • Date: Tue, 9 Mar 2004 04:30:51 -0500 (EST)
  • Sender: owner-wri-mathgroup at

>-----Original Message-----
>From: Harold.Noffke at [mailto:Harold.Noffke at]
To: mathgroup at
>Sent: Monday, March 08, 2004 10:10 AM
>To: mathgroup at
>Subject: [mg46808] [mg46788] ReplaceList -- Unexpected Answer
>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]] }.


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}]
{{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}]
{{r[h[4]], f[h[5], h[5]]}, {f[h[4], h[4]], r[h[5]]}}


  • 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?