Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1997
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1997

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

Search the Archive

Re: pattern matching

  • To: mathgroup at smc.vnet.net
  • Subject: [mg7026] Re: [mg6967] pattern matching
  • From: Rick Muirhead <muirhead at dirac.phys.washington.edu>
  • Date: Fri, 2 May 1997 21:30:52 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com


On Wed, 30 Apr 1997, Marc Mazzariol wrote:

> Hello,
> 
> I'm using mathematica 2.2 (Unix), and i'm trying to do pattern 
> matching with repeated patterns.
> 
> This example produce the result i want :
> 
> 	In :  {a,a,a,b} /. {a...,b} -> Hello
> 	Out : Hello
> 
> But this one don't :
> 	
> 	In : {b} /. {a...,b} -> Hello
> 	Out : {b}
> 
> I was hopping :     Out : Hello
> 
> Why doesn't work it ???
> 
> -- 
> +--------------------------------------------------------+
> |               - No brain, no headache -                |
> +--------------------------------------------------------+
> |    Marc Mazzariol                       		 |
> |    Swiss Federal Institute of Technology (EPFL) 	 |
> |    Peripheral Systems Laboratory (LSP)		 |
> |    CH-1015 Lausanne, Switzerland			 |
> |    Tel: +41 21 693 3944				 |
> |    Fax: +41 21 693 6680				 |
> |    E-mail: Marc.Mazzariol at di.epfl.ch        		 |
> +--------------------------------------------------------+
> 

Greetings Marc,


  In general, I get some insight, however tiny, on how the patterns do or 
do not match by playing with MatchQ.  In your case, we find

In[1]:=
        MatchQ[ {b}, {a...,b}]

Out[1]:=
        False

                
whereas:


In[2]:=
        MatchQ[{a,a,a,a,a,a,b},{a...,b}]

Out[2]:=
        True


Similarly, your pattern won't recognize {b,a,a,a,a,a}


Something that does work, though I suspect there are more elegant 
solutions, would be

In[3]:=
          {a,a,a,a,a,a,b} /.{a...,b} | {b}->Hello

Out[3]:=
          Hello


In[4]:=
          {b}/.{a...,b} | {b}->Hello

Out[4]:=
         Hello


 The | sign is Mma's pattern alternative sign ... I think of it as an OR 
within patterns. 

 Off the subject ....  if your purpose is to determine if the symbol b is 
a member of your list, you might instead try the MemberQ function, which 
I find a good deal more transparent then the pattern matching:


In[5]:=
       MemberQ[{a,a,a,a,a,b},b]

Out[5]:= 
       True


In[6]:=
       MemberQ[{a,a,a,a,a},b]

Out[6]:= 
       False



  

Rick Muirhead
Physics UW


  • Prev by Date: Re: Help ! complex permutations
  • Next by Date: Re: Comfortable/Appropriate processor for Mathematica 3.0
  • Previous by thread: Re: pattern matching
  • Next by thread: Re: Help!!!!!!