More Pattern Match Understanding Problems
- To: mathgroup at smc.vnet.net
- Subject: [mg46620] More Pattern Match Understanding Problems
- From: Harold.Noffke at wpafb.af.mil (Harold Noffke)
- Date: Wed, 25 Feb 2004 13:07:12 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
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