MathGroup Archive 2004

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

Search the Archive

Re: HoldPattern & Pattern Matching

  • To: mathgroup at smc.vnet.net
  • Subject: [mg50478] Re: [mg50473] HoldPattern & Pattern Matching
  • From: Andrzej Kozlowski <andrzej at akikoz.net>
  • Date: Mon, 6 Sep 2004 03:59:36 -0400 (EDT)
  • References: <200409050753.DAA29807@smc.vnet.net>
  • Reply-to: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Sender: owner-wri-mathgroup at wolfram.com

On 5 Sep 2004, at 16:53, Paul Buettiker wrote:

>
> Dear all,
>
> I don't understand the following behavior of Mathematica 4.2:
>
>
> In[1]:= $Version
> Out[1]= 4.2 for Linux (August 23, 2002)
>
> In[2]:= mysum = h[a] - 7h[b]
> Out[2]= h[a] - 7 h[b]
>
> In[3]:= mysum /. HoldPattern[Plus[aaa : (_.*h[_]) ..]] :> aaa
> Out[3]= h[a] - 7 h[b]
>
> In[4]:= mysum /. HoldPattern[Plus[aaa : (_.*h[_]) ..]] :> aaa // Trace
> Out[4]= {{mysum, h[a] - 7 h[b]},
>>    h[a] - 7 h[b] /. HoldPattern[Plus[aaa:((_.) h[_]..)]] :> aaa,
>>    Plus[Sequence[h[a], -7 h[b]]], h[a] - 7 h[b]}
>
>
> The pattern matching should work for an arbitrary number of terms,
> that's why I used Repeated[...], i.e. the "..".
>
> I hoped to get Sequence[ h[a], -7 h[b] ].   I dont understand why
> Sequence[...] is wrapped by Plus[...]. What did I miss?
>
> Many thanks!
> Paul.
>
>
This works:


Replace[mysum,HoldPattern[Plus[aaa : (_.*h[_]) ..]] :> aaa]


Sequence[h[a],-7 h[b]]

The whole point lies inthe difference between Replace and ReplaceAll. 
Here is the documentation for ReplaceALl:

ReplaceAll looks at each part of expr, tries all the rules on it, and 
then goes on to the next part of expr. The first rule that applies to a 
particular part is used; no further rules are tried on that part, or on 
any of its subparts.

I hope this makes it clear.



Andrzej Kozlowski
Chiba, Japan
http://www.mimuw.edu.pl/~akoz/


  • Prev by Date: not a floating number , gptn complains...
  • Next by Date: Re: newbie is looking for a customDistribution function
  • Previous by thread: HoldPattern & Pattern Matching
  • Next by thread: Install Fix for Mathematica 5.0.1 Font on Linux Xorg/Fedora Core2