Re: Pattern matching more than once
- To: mathgroup@smc.vnet.net
- Subject: [mg12040] Re: [mg11991] Pattern matching more than once
- From: Wouter Meeussen <w.meeussen.vdmcc@vandemoortele.be>
- Date: Fri, 24 Apr 1998 01:52:11 -0400
hi,
maybe the following rule would do what you want, even if there are
*more* than three common elements:
myrule={a___,list1:{pre1___,x_,post1___},
list2:{pre2___,x_,post2___},b___} ->
{z[{a,{pre1,y[x],post1},list2,b}] ,
z[{{pre1,y[x],post2},{pre2,x,post1}}]};
postop[it_]:= Flatten[k= (it/. z:>Sequence /.y:>Identity) ,Depth[k]-3]
//. is ReplaceRepeated,
// is postfix-Operation.
{{a,b,c},{d,b,f}}//.rule // postop
Out[ ]=
{{a, b, c}, {d, b, f}, {a, b, f}, {d, b, c}}
{{a,b,c,d,e},{f,b,g,d,h}}//.rule //postop Out[]=
{{a, b, c, d, e}, {f, b, g, d, h}, {a, b, c, d, h}, {f, b, g, d, e},
{a, b, g, d, h}, {f, b, c, d, e}, {a, b, g, d, e}, {f, b, c, d, h}}
{{a,b,c,d,e,u,v},{f,b,g,d,h,u,x}}//.rule //postop Out[]=
{{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:40 17.04.98 -0400, you wrote:
>I have been stuck with the following problem for about 10 days. I can't
>think of an elegant solution.
>
>Input: Two lists, from some universal set Output: Many "spliced" lists
>obtained from the input.
>
>e.g. {{a,b,c},{d,e,f}} --> {{a,b,c},{d,e,f}} (no common element)
>
> {{a,b,c},{d,b,f}} --> {{a,b,c},{d,b,f},{a,b,f},{d,b,c}}
>
> {{a,b,c,d,e},{f,b,g,d,h}} -->
> {{a,b,c,d,e},{a,b,c,d,h},{a,b,g,d,e},{a,b,g,d,h},
> {f,b,c,d,e},{f,b,c,d,h},{f,b,g,d,e},{f,b,g,d,h}}
>
>I will regard the input as list of chains (totally ordered set) coming
>from a partially ordered set, and I would like to extend these chains
>as much as possible. If there is at most one common element in the
>inputs, the following will do:
>
>extendList[{list1:{pre1___,x_,post1___},list2:{pre2___,x_,post2___}}]:=
> {list1,list2,{pre1,x,post2},{pre2,x,post1}};
>
>The problem is that when the lists have more than one common element,
>the second common element will not be considered at all. I suppose I
>could just write, for instance,
>
>extendList2[{list1:{pre1___,x_,mid1___,y_,post1___},
> list2:{pre2___,x_,mid2___,y_,post2___}}]:=
> Union[
> {list1,{pre1,x,mid1,y,post2},{pre1,x,mid2,y,post1},
> {pre1,x,mid2,y,post2},list2,{pre2,x,mid1,y,post1},
> {pre2,x,mid1,y,post2},{pre2,x,mid2,y,post1}}]; But this is very ugly.
>
>I suppose I could write some For[] loop through
>Intersection[list1,list2], but I would rather avoid it if I could.
>
>Any help would be appreciated.
>
>--
>ASARI Hirotsugu // http://www.math.uiuc.edu/~asari/
>finger://math.uiuc.edu/asari // ph://ns.uiuc.edu/asari
> "We are what we pretend to be, so we must be careful
> about what we pretend to be." --Kurt Vonnegut
>
>
NV Vandemoortele Coordination Center Oils & Fats Applied Research
Prins Albertlaan 79
Postbus 40
B-8870 Izegem (Belgium)
Tel: +/32/51/33 21 11
Fax: +/32/51/33 21 75
vdmcc@vandemoortele.be