Re: [correction] Pattern matching more than once
- To: mathgroup@smc.vnet.net
- Subject: [mg12258] Re: [mg12217] [correction] Pattern matching more than once
- From: Wouter Meeussen <eu000949@pophost.eunet.be>
- Date: Tue, 5 May 1998 03:29:54 -0400
Robert,
could you re-mail your [correction] please? I received it with a lot of
spurious " ÿ "-characters in it.
I would like to check if my version comes to the same result as yours:
myruleú___,list1:{pre1___,x_,post1___},
list2:{pre2___,x_,post2___},b___} ->
{zdummy[{a,{pre1,ydummy[x],post1},list2,b}] ,
zdummy[{{pre1,ydummy[x],post2},{pre2,x,post1}}]};
postop[it_]:ÿ
Flatten[kÿit/. zdummy:>Sequence /.ydummy:>Identity) ,
Depth[k]-3+0*Clear[k]]
In[58]:lÿa,b,c,d,e,u,v},{f,b,g,d,h,u,x}};
In[59]:l //. myrule //postop
Out[59]{{a,b,c,d,e,u,v},{f,b,g,d,h,u,x},{a,b,c,d,e,u,x},{f,b,g,d,h,u,v},{a,b,c,d ,h,u,
x},{f,b,g,d,e,u,v},{a,b,c,d,h,u,v},{f,b,g,d,e,u,x},{a,b,g,d,h,u,x},{f ,b,c,
d,e,u,v},{a,b,g,d,h,u,v},{f,b,c,d,e,u,x},{a,b,g,d,e,u,v},{f,b,c,d,h,u ,x},{
a,b,g,d,e,u,x},{f,b,c,d,h,u,v}}
wouter.
*************************************************************************
*****
At 03:09 1-05-98 -0400, Robert Villegas wrote:
>Thanks to Jrgen Tischer for prompting me to think about some more
>examples, and discovering that my code had a bug in it. In the
>auxiliary function Interleave, I applied Drop and Join in the wrong
>order.
>
>Here is a correction, along with a couple examples that came out of my
>correspondence with Jrgen, which work fine with the new code. I also
>added an order-preserving Union called EliminateRepetition, since
>sometimes duplicates end up in the result.
>
>
>Interleave[subLists_, commonElements_] /;
> (Length[commonElements] ÿLength[subLists] - 1) : Join @@ Drop[#, -1]& @
> MapThread[Sequence,
> {subLists, List /@ Append[commonElements, Null]}]
>
>EliminateRepetition[list_List] :ÿast /@
> Sort[ {Position[list, Verbatim[#], {1}, 1][[1, 1]], #}& /@ Union[list]
>ÿ
>
>SpliceChains[chain1_, chain2_] : Module[{commonElements, commonPos1,
commonPos2, subChains1,
>subChains2}ÿ
> commonElements ÿntersection[chain1, chain2];
> commonPos1 Flatten @
> Position[chain1, Alternatives @@ commonElements, {1},
> Heads -> False];
> commonPos2 Flatten @
> Position[chain2, Alternatives @@ commonElements, {1},
> Heads -> False];
> subChains1 úpply[
> Take[chain1, {#1 + 1, #2 - 1}] &,
> Partition[Join[{0}, commonPos1, {0}], 2, 1],
> {1}
> ];
> subChains2 úpply[
> Take[chain2, {#1 + 1, #2 - 1}] &,
> Partition[Join[{0}, commonPos2, {0}], 2, 1],
> {1}
> ];
> EliminateRepetition @
> Distribute[Transpose[{subChains1, subChains2}], List, List,
> List,
> Interleave[{##}, commonElements] &
> ]
> ]
>
>
>(* Examples that broke my original code, but work fine in the new code:
>*ÿ
>
>In[10]:ÿpliceChains[{a, x}, {b, x, c}]
>
>Out[10]ÿ{a, x}, {a, x, c}, {b, x}, {b, x, c}}
>
>In[11]:ÿpliceChains[{{a}, {a, b, c}, {a, b, c, d, e}},
> {{a, b}, {a, b, c}, {a, b, c, d}}]
>
>Out[11]ÿ{{a}, {a, b, c}, {a, b, c, d, e}}, {{a}, {a, b, c}, {a, b,
>c, d}},
> {{a, b}, {a, b, c}, {a, b, c, d, e}}, {{a, b}, {a, b, c}, {a, b, c,
>d}}ÿ
>
>
>I'm sorry for the error in the previous version.
>
>
>Robby Villegas
>
>P.S. By the way, as an advanced note, if the elements of your sets can
>bþ
>Mathematica patterns such as x_, which is unlikely, then my code won't
>work. This could be fixed quite easily. Also, if you're doing
>anything really tricky with unevaluated expressions, then it won't
>work. This could be fixed, but less easily. Unless you're doing
>something for an esoteric programming purpose, these limitations won't
>bother you.
>
>
Dr. Wouter L. J. MEEUSSEN
w.meeussen.vdmcc@vandemoortele.be
eu000949@pophost.eunet.be