summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace/sicp/two/thirtytwo.scm
blob: d189963e012f9406b4208d2ecc26df7555f36303 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
;;;; License: CC0-1.0

(define-module (net ricketyspace sicp two thirtytwo)
  #:export (subsets))

(define (subsets s)
  "All subsets of a Set X (P) is the union of all subsets of CDR of
Set X (Q) and a subset R derived from the union of CAR X with each of
the subsets in Q."
  (if (null? s)
      (list '())
      (let ((rest (subsets (cdr s))))
        (append rest (map (lambda (item) (cons (car s) item)) rest)))))

;;; Guile REPL
;;;
;;; scheme@(guile-user)> ,use (net ricketyspace sicp two thirtytwo)
;;; scheme@(guile-user)> (subsets '(1 2 3))
;;; $6 = (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))