········@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.
>
>Thanx
>Jonavan
Try the following function:
(defun power (s)
(if (null s)
(list ())
(let ((power_tail (power (cdr s))))
(append
(mapcar #'(lambda (x)
(cons (car s) x))
power_tail)
power_tail))))
Example and explanation:
(POWER '()) -> ()
(POWER '(1)) -> (1) ()
(POWER '(2 1)) -> (2 1) (2) (1) ()
(POWER '(3 2 1)) -> (3 2 1) (3 2) (3 1) (3) (2 1) (2) (1) ()
I think, it's correct and hope that helps.
Michael
--
Michael Wullenweber ···@ThinkTools.com