MathGroup Archive 2004

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

Search the Archive

Re: Baffled By Underscore Pattern Matching

  • To: mathgroup at
  • Subject: [mg46024] Re: [mg45992] Baffled By Underscore Pattern Matching
  • From: "Sseziwa Mukasa,,(978) 536-2359" <mukasa at>
  • Date: Tue, 3 Feb 2004 03:20:55 -0500 (EST)
  • References: <>
  • Sender: owner-wri-mathgroup at

On Feb 2, 2004, at 5:20 AM, Harold Noffke wrote:

> 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.

I think one can understand this function just by carefully tracing the 
program.  First of all, one must understand the definition of h.  h 
takes an arbitrary number of arguments.  Reading the arguments from 
left to right, the first repeated element that's encountered is bound 
to x.  a, b, and c are then those arguments which separate the repeated 
element.  h is also recursive, calling itself again with the argument 
a,b,c.  Your modification on line 5, changed h such that it calls 
itself on the arguments {a},{b},{c}, which is three lists.  Given the 
original argument 2,3,2,4,5,3, x is bound to 2, the first repeated 
argument, a is then empty, b is 3 and c is 4,5,3.  In your modified 
version h is thus called with the argument {},{3},{4,5,3}.  None of the 
elements are repeated and recursion terminates with your result.  The 
original function is called with the argument 3,4,5,3, the empty 
argument a being dropped, so x binds to 3, a is empty again, b is 4,5 
and c is empty.  On the recursion h is called with 4,5 which has no 
repeated element, so recursion terminates with the result seen in 

Hope that's clear enough,


  • Prev by Date: RE: Drawing specific contours in a 3D Surface Plot?
  • Next by Date: Re: Baffled By Underscore Pattern Matching
  • Previous by thread: Baffled By Underscore Pattern Matching
  • Next by thread: Re: Baffled By Underscore Pattern Matching