[correction] Pattern matching more than once
- To: mathgroup@smc.vnet.net
- Subject: [mg12217] [correction] Pattern matching more than once
- From: Robert Villegas <villegas@wolfram.com>
- Date: Fri, 1 May 1998 03:09:04 -0400
- Organization: Wolfram Research
- References: <353E755E.24F7F30F@wolfram.com>
Thanks to Jürgen 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 Jürgen, 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.