• To: mathgroup at smc.vnet.net
• Subject: [mg46822] Re: ReplaceList -- Unexpected Answer
• From: drbob at bigfoot.com (Bobby R. Treat)
• Date: Tue, 9 Mar 2004 04:31:12 -0500 (EST)
• References: <c2hdqc\$aeq\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```ReplaceList (contrary to the documentation in my opinion) tries to
transform the expression at its top level only; it doesn't apply rules
to subexpressions.

{f[h[4],h[4]],f[h[4],h[5]]};
ReplaceList[# ,f[x:h[_],x_] -> r[x] ]&/@%

{{r[h[4]]},{}}

or

g@f[x : h[_], x_] := r[x]
g@a_ := a
g //@ {f[h[4], h[4]], f[h[4], h[5]]}

{r[h[4]], f[h[4], h[5]]}

Bobby

Harold.Noffke at wpafb.af.mil (Harold Noffke) wrote in message news:<c2hdqc\$aeq\$1 at smc.vnet.net>...
> 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

```

• Prev by Date: Re: Problem with Mathematica driving me nuts
• Next by Date: Re: Shared variables between notebooks?