(リストを集合とみなして)
(use srfi-1) ;; append-map (define (my-power-set set) (if (null? set) '(()) (append-map (lambda (subset) `((,@subset ,(car set)) ,subset)) (my-power-set (cdr set)))))
gosh> (my-power-set '(a b c d e)) ((e d c b a) (e d c b) (e d c a) (e d c) (e d b a) (e d b) (e d a) (e d) (e c b a) (e c b) (e c a) (e c) (e b a) (e b) (e a) (e) (d c b a) (d c b) (d c a) (d c) (d b a) (d b) (d a) (d) (c b a) (c b) (c a) (c) (b a) (b) (a) ())
だいたいあってる
で、
(define (my-power-set* set) (if (null? set) '(()) (append-map (lambda (subset) `((,(car set) ,@subset) ,subset)) (my-power-set* (cdr set)))))
とやると
gosh> (my-power-set '(a b c)) ((c b a) (c b) (c a) (c) (b a) (b) (a) ()) gosh> (my-power-set* '(a b c)) ((a . #0=(b . #1=(c))) #0# (a . #1#) #1# (a . #2=(b)) #2# (a) ())
これではまったけどリストの構造を考えたら理解でけた…ような…