Re: are nested patterns impossible?
- To: mathgroup@smc.vnet.net
- Subject: [mg11300] Re: [mg11203] are nested patterns impossible?
- From: Wouter Meeussen <eu000949@pophost.eunet.be>
- Date: Wed, 4 Mar 1998 01:39:58 -0500
Thomas,
does this help any:
In[202]:=pat= x_. u + y_:0 v
{MatchQ[u+10 v,pat],
MatchQ[2 u+3v,pat],
MatchQ[u*5,pat],
MatchQ[u +5,pat],
MatchQ[2+v ,pat]}
Out[202]=u x_.+y_:0
Out[203]={True,True,True,True,False}
In[206]:=pft=u * x_. +v* y_:0
{MatchQ[u+10 v,pft],
MatchQ[2 u+3v,pft],
MatchQ[u*5,pft],
MatchQ[u+5 ,pft],
MatchQ[2+v ,pft]}
Out[206]=u x_.+v (y_:0)
Out[207]={True,True,False,False,False}
Should we call this "the limits of commutativity in patterns"?
wouter.
At 23:10 2-03-98 -0500, Thomas Lemm wrote:
>I try to create a pattern describing a linear combination of a list of
>variables. Be those variables u and v, the pattern should match for
>both
>
>u*k+v*l (k,l constants)
>
>and
>
>u*k (coefficient of v == 0)
>
>But when I program it in using nested patterns
>
>In[82]:=pat=u*_.+(v*_.|0)
>Out[82]=(v _.|0)+u _.
>
>In[84]:=MatchQ[u*5+v*10,pat]
>Out[84]=True
>
>In[86]:=MatchQ[u*5,pat]
>Out[86]=False
>
>The last case does not match, which leads to the question: what did I
>program and how can I program the thing I intended to?
>
>I already know about the possibility making a pattern like
>
>u*_.+v*_.|u*_.
>
>But this is not the way I want to program this example as I would like
>to extend this case to many more than two variables u,v.
>
>Thomas Lemm
>
>
Dr. Wouter L. J. MEEUSSEN
w.meeussen.vdmcc@vandemoortele.be
eu000949@pophost.eunet.be