       Re: Baffled By Underscore Pattern Matching

• To: mathgroup at smc.vnet.net
• Subject: [mg46085] Re: Baffled By Underscore Pattern Matching
• From: drbob at bigfoot.com (Bobby R. Treat)
• Date: Thu, 5 Feb 2004 04:03:04 -0500 (EST)
• References: <bvl8su\$t50\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Here's a version that selects the largest possible x:

Clear[h]
h[a___, x_, b___, x_,
c___] /; x ==
Max[Select[{a, x, b, c},
Count[{x, a, b, x, c},
#1] > 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
h[{}, {3}, {4, 5, 7}]*hh
h[{}, {3}, {4, 5, 3}]*hh

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/Out example I understand, but the
> In[2,3]/Out example (discussed below) has me totally mystified.
>
> As printed, we have ...
>
> 	In:= h[a___, x_, b___, x_, c___] := hh[x] h[a, b, c]
>
> 	In:= h[2, 3, 2, 4, 5, 3]
>
> 	Out= h[4, 5] hh hh
>
> Now let's make a change to In ...
>
> 	In:= Clear[h, hh]
>
> 	In:= h[a___, x_, b___, x_, c___] := hh[x] h[{a}, {b}, {c}]
>
> 	In:= h[2, 3, 2, 4, 5, 3]
>
> 	Out= h[{}, {3}, {4, 5, 3}] hh
>
> 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 manipulates the number
> stream which feeds into it from In.  I have no idea of how the
> In lists came to contain the numbers they do.
>
> Any help, pointers to tutorial papers, or more illuminating examples
> will be greatly appreciated.
>
> Thanks.
> Harold

```

• Prev by Date: Re: MathML Namespace + Mathematica
• Next by Date: Re: Baffled By Underscore Pattern Matching
• Previous by thread: RE: Baffled By Underscore Pattern Matching
• Next by thread: Re: Baffled By Underscore Pattern Matching