Re: Subsets

• To: mathgroup at smc.vnet.net
• Subject: [mg4987] Re: Subsets
• From: Clemens Frey <Clemens.Frey at uni-bayreuth.de>
• Date: Thu, 17 Oct 1996 00:41:03 -0400
• Organization: uni-bayreuth.de
• Sender: owner-wri-mathgroup at wolfram.com

```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}} .

Clemens

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

```

• Prev by Date: Re: Transformation rule exercise
• Next by Date: Re: Conjugate