MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Pattern matching more than once


  • To: mathgroup@smc.vnet.net
  • Subject: [mg12096] Re: Pattern matching more than once
  • From: Paul Abbott <paul@physics.uwa.edu.au>
  • Date: Sat, 25 Apr 1998 01:30:28 -0400
  • Organization: University of Western Australia
  • References: <6hjqaa$2kj@smc.vnet.net>

ASARI Hirotsugu 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 think that the following code:

In[1]:=extendList[l_List]:= Module[{tr=Union/@Transpose[l]},
    If[tr==Transpose[l],l,tr=Outer[List,Sequence@@tr];
      Flatten[tr,Length[Dimensions[tr]]-2]]]

which relies on Transpose (to pair up elements from each list) Union (to
check for common elements) and Outer (to construct all allowed
combinations) does what you want:

In[2]:= extendList[{{a,b,c},{d,e,f}}] Out[2]= {{a,b,c},{d,e,f}}
In[3]:= extendList[{{a, b, e}, {d, b, f}}] Out[3]=
{{a,b,e},{a,b,f},{d,b,e},{d,b,f}} In[4]:=
extendList[{{a,b,c,d,e},{f,b,g,d,h}}] Out[4]=
{{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 also feel that there should be a cleverer pattern-matching solution,
probably using ReplaceList ...

Cheers,
	Paul 

____________________________________________________________________ 
Paul Abbott                                   Phone: +61-8-9380-2734
Department of Physics                           Fax: +61-8-9380-1014
The University of Western Australia            Nedlands WA  6907       
mailto:paul@physics.uwa.edu.au  AUSTRALIA                            
http://www.pd.uwa.edu.au/~paul

            God IS a weakly left-handed dice player
____________________________________________________________________



  • Prev by Date: Re: BesselJZeros strangeness
  • Next by Date: Re: How to get the solution area of inequation?
  • Prev by thread: Re: Pattern matching more than once
  • Next by thread: Output of Animate to AVI