Re: simultaneous ... and ___
- To: mathgroup at smc.vnet.net
- Subject: [mg60920] Re: simultaneous ... and ___
- From: Maxim <ab_def at prontomail.com>
- Date: Mon, 3 Oct 2005 04:06:26 -0400 (EDT)
- References: <dhj4ll$smo$1@smc.vnet.net><dhlc4l$cuj$1@smc.vnet.net> <dhnso6$1eu$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Sun, 2 Oct 2005 05:55:18 +0000 (UTC), borges2003xx at yahoo.it <borges2003xx at yahoo.it> wrote: > sorry but for a refuse (copy and paste ) i write > m1[{___, x1__ .., x2___ ..., x3___ ..., ___, x3___ ..., x2___ ..., > x1__ .., ___}] := {} > /; (Length[{x1}] + Length[{x1}] + Length[{x1}]) >= 15 > m1[l1_] := l1 > > the real version is > m1[{___, x1__ .., x2___ ..., x3___ ..., ___, x3___ ..., x2___ ..., > x1__ .., ___}] := {} > /; (Length[{x1}] + Length[{x2}] + Length[{x3}]) >= 15 (or 5 or...) > m1[l1_] := l1 > > i apologies > i use triple ___ for x2 and x3 cause i don't want to be filled always > with something > The key point seems to be that Repeated cannot repeat sequences (BlankSequence or another Repeated): In[1]:= MatchQ[{a, b, a, b}, {(x__)..}] Out[1]= False In principle there could be a match if x were set to Sequence[a, b]. But Mathematica pattern matching doesn't do that (string patterns are different in that aspect). The pattern (x__).. can be matched only if x can be set to a sequence of length 1: In[2]:= MatchQ[{a, a}, {(x__)..} /; Length@ {x} == 1] Out[2]= True In[3]:= MatchQ[{a, a}, {(x__)..} /; Length@ {x} == 2] Out[3]= False As we can see, x can be set to a, which provides a match, but not to Sequence[a, a]. In my opinion, this gives the user the wrong impression that sequences can be repeated, when in fact this isn't quite so. This leads to certain inconsistencies: In[4]:= MatchQ[{a, a}, {x__, (x__)..}] Out[4]= False In[5]:= MatchQ[{a, a}, {x__, (y__)..} /; {x} === {y}] Out[5]= True It is hard to explain why the patterns x and y cannot have the same name, if they can match identical subexpressions (this also shows that it is not clear how this construct works even with one-element sequences). One might try to use (x_).. inside a Flat function (say, Plus) to make Mathematica automatically group a sequence inside Plus so that it can be matched by x_ instead of x__. But that doesn't work either: In[6]:= MatchQ[Hold[a + b + a + b], Hold[Plus[x_, x_]]] Out[6]= True In[7]:= MatchQ[Hold[a + b + a + b], Hold[Plus[(x_)..]]] Out[7]= False Mathematica rewrites the expression as Plus[Plus[a, b], Plus[a, b]] only in the first example. One way around this is to use the sequence x___, y___ and add a test to verify that y comprises repetitions of x. Also nested repeats of that kind can be done via string patterns: In[8]:= StringCases[ StringJoin[ToString /@ {0,1,1,2,2,2,3,4,3,3,3,3,2,1,1,1,1,8}], x1__..~~x2__..~~x3__..~~___~~x3__..~~x2__..~~x1__.. /; StringLength[x1~~x2~~x3] >= 4 :> {x1, x2, x3}] Out[8]= {{"11", "2", "3"}} This correctly determines that x1 can be set to "11" (repeated once before x2 and twice after x2), and so on. I used __.. to avoid trivial matches, e.g., all elements of the pattern except one set to empty strings. Maxim Rytin m.r at inbox.ru