MathGroup Archive 2004

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

Search the Archive

Re: More Pattern Match Understanding Problems

  • To: mathgroup at
  • Subject: [mg46636] Re: More Pattern Match Understanding Problems
  • From: drbob at (Bobby R. Treat)
  • Date: Thu, 26 Feb 2004 17:53:24 -0500 (EST)
  • References: <c1ir85$6b1$>
  • Sender: owner-wri-mathgroup at

Your adding "<labeled?>" to the definition is incorrect. In the first
example, there are no unlabeled patterns, so your definition couldn't

In the third example, the shortest possible match to ____ (three
blanks) is an empty set, so x matches beginning with the first
element. But the rule applies again, so it matches with the shortest
sequence starting there -- just a. It doesn't match all the rest of
the elements, because the third pattern ___ is available to do that.

In the second example, the first pattern ____ (three blanks) again
matches an empty set, and x__ matches all the rest of the arguments.
As you see.


Harold.Noffke at (Harold Noffke) wrote in message news:<c1ir85$6b1$1 at>...
> MathGroup:
> In the Mathematica 5 Book, Section 2.3.8, "Functions with Variable
> Numbers of Arguments," there are three examples of using ReplaceList
> to understand pattern matching (In/Out-4,5,6).  I understand In/Out-4
> and In/Out-6 if I assume the Mathematica explanation (immediately
> below) refers only to "labeled blanks" when it says "blanks" ...
>     When you use multiple blanks, there are often several matches that
>     are possible for a particular expression.  In general, Mathematica
>     tries first those matches that assign the shortest sequences of
>     arguments to the first <labeled?> multiple blanks that appear in
>     the pattern.
> When I examine In/Out-5, however, my understanding breaks down when I
> see that the answer Mathematica gives is g[a,b,c,d], which is the
> longest (not the shortest) sequence assigned to the labeled blank x__.
>     In[5]:=
>         ReplaceList[f[a, b, c, d], f[___, x__] -> g[x]]
>     Out[5]=
>         {g[a,b,c,d], g[b,c,d], g[c,d], g[d]}
> Therefore, I conclude my reasoning is incorrect.
> Can anyone provide some guidance on how to think through In/Out-4,5,6
> without assuming labeled multiple-blanks have priority over unlabeled
> ones?
> Thanks.
> Harold

  • Prev by Date: RE: Matrix Element Extraction
  • Next by Date: RE: More Pattern Match Understanding Problems
  • Previous by thread: More Pattern Match Understanding Problems
  • Next by thread: RE: More Pattern Match Understanding Problems