Re: Baffled By Underscore Pattern Matching
- To: mathgroup at smc.vnet.net
- Subject: [mg46086] Re: Baffled By Underscore Pattern Matching
- From: drbob at bigfoot.com (Bobby R. Treat)
- Date: Thu, 5 Feb 2004 04:03:05 -0500 (EST)
- References: <bvl8su$t50$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Or better yet, if we wanted x to be as large as possible:
Clear@h
h[a___, x_, b___, x_, c___] /; x >= Max@Select[List[a, b, c], Count[
List[a, b, c], #] > 1 &] := hh[x] h[{a}, {b}, {c}]
h[2, 3, 2, 4, 5, 3]
h[2, 3, 2, 4, 5, 7]
h[8, 3, 8, 4, 5, 3]
h[{2},{2,4,5},{}] hh[3]
h[{},{3},{4,5,7}] hh[2]
h[{},{3},{4,5,3}] hh[8]
Bobby
Harold.Noffke at wpafb.af.mil (Harold Noffke) wrote in message news:<bvl8su$t50$1 at smc.vnet.net>...
> MathGroup:
>
> In my study of Mathematica 5.0, I have reached "The Mathematica Book >
> Principals of Mathematica > Patterns > 2.3.8 Functions with Variable
> Numbers of Arguments". The In[1]/Out[1] example I understand, but the
> In[2,3]/Out[3] example (discussed below) has me totally mystified.
>
> As printed, we have ...
>
> In[2]:= h[a___, x_, b___, x_, c___] := hh[x] h[a, b, c]
>
> In[3]:= h[2, 3, 2, 4, 5, 3]
>
> Out[3]= h[4, 5] hh[2] hh[3]
>
> Now let's make a change to In[2] ...
>
> In[4]:= Clear[h, hh]
>
> In[5]:= h[a___, x_, b___, x_, c___] := hh[x] h[{a}, {b}, {c}]
>
> In[6]:= h[2, 3, 2, 4, 5, 3]
>
> Out[6]= h[{}, {3}, {4, 5, 3}] hh[2]
>
> I did a Trace on this pattern match problem, and found only that
> doublets were pulled out on each iteration. In order to understand
> what is happening here, I think I need to understand the matching
> process at a level of granularity finer than Trace can supply. I
> don"t have a clear mental picture of how In[5] manipulates the number
> stream which feeds into it from In[6]. I have no idea of how the
> In[6] lists came to contain the numbers they do.
>
> Any help, pointers to tutorial papers, or more illuminating examples
> will be greatly appreciated.
>
> Thanks.
> Harold