MathGroup Archive 1996

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

Search the Archive

Re: Subsets


Fabiana Costa Pereira wrote:
> 
>   I need to know if there is a function in Mathematica
> that generates the set of all subsets of a list.

Hi,
as far as i am concerned, there is no such function. But I had that
problem myself some time ago, and here is how I solved it :

addEl[ set_List, el_ ] := Flatten[ {#,Flatten[{#,el}]}& /@ set, 1 ];
allSs[ set_List ] := Fold[ addEl, {{}}, Union[set] ];

Then you can use allSs[...] to obtain a list of all subsets of
a given set. Because you cannot be sure that a list contains all its
elements exacly once, I use Union[set] to get an (ordered) list
of all elements of set. So you can type in this:

IN>> allSs[ {1,2,3} ] //Union

...and you will get :

OUT>> {{},{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}}

Of course, you can also use symbols instead of numbers as elements of 
your list. Because I used Union[set] in the definition of allSs, the
following 
IN>> allSs[ {a,a} ]          
will result in
OUT>> {{},{a}}
and not in
NOT OUT>> {{},{a},{a},{a,a}} .

Hope I could help you
Clemens

(-: Clemens.Frey at uni-bayreuth.de :-)


  • Prev by Date: Re: Transformation rule exercise
  • Next by Date: Re: Conjugate
  • Previous by thread: Subsets
  • Next by thread: Re: Re: Subsets