HoldPattern & Pattern Matching
- To: mathgroup at smc.vnet.net
- Subject: [mg50473] HoldPattern & Pattern Matching
- From: Paul Buettiker <buettike at itkp.uni-bonn.de>
- Date: Sun, 5 Sep 2004 03:53:49 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
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.
- Follow-Ups:
- Re: HoldPattern & Pattern Matching
- From: Andrzej Kozlowski <andrzej@akikoz.net>
- Re: HoldPattern & Pattern Matching