MathGroup Archive 1996

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

Search the Archive

Re: Subsets

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

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.

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
IN>> allSs[ {a,a} ]          
will result in
OUT>> {{},{a}}
and not in
NOT OUT>> {{},{a},{a},{a,a}} .

Hope I could help you

(-: Clemens.Frey at :-)

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