Newsgroups: comp.lang.lisp
From: ward@mondas.demon.co.uk (Peter Ward)
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!howland.reston.ans.net!EU.net!news.sprintlink.net!peernews.demon.co.uk!mondas.demon.co.uk!ward
Subject: Re: Power
References: <3j198s$4ig@itu1.sun.ac.za>
Organization: Mondas
Reply-To: ward@mondas.demon.co.uk
X-Newsreader: Demon Internet Simple News v1.27
Lines: 29
X-Posting-Host: mondas.demon.co.uk
Date: Thu, 2 Mar 1995 07:43:13 +0000
Message-ID: <794130193snz@mondas.demon.co.uk>
Sender: usenet@demon.co.uk

In article <3j198s$4ig@itu1.sun.ac.za> s9231765@cs.sun.ac.za "J Jacobs" writes:

> Can someone tell me how to impliment a function power set of 
> a set in Common Lisp. 
>   The power set of eg. '(a b c) is 
>   '((a b c) (a b) (a c) (b c) (a) (b) (c) nil)
> It should work for any number of elements in a set.

I'm no Lisp programmer but you just need to think how to do it
recursively. To work through your example...

To get the power set of '(a b c), you could start with
the power set of '(b c). You are allowed to think like this
when writing recursive functions. This gives you
  '((b c) (b) (c) nil)
Now if you made a copy of that with 'a added into each member, you get
  '((a b c) (a b) (a c) (a))
Join those together and
  '((a b c) (a b) (a c) (a) (b c) (b) (c) nil)
Which is right unless the order is important to you.
The recursion terminates because the power set of nil is (nil).

BTW I suspect there is a neater, more efficient way but this
makes the thought process clear.

-- 

Pete Ward
Mondas IT Ltd
