Blob


1 (define (test-case actual expected)
2 (newline)
3 (display "Actual: ")
4 (display actual)
5 (newline)
6 (display "Expected: ")
7 (display expected)
8 (newline))
10 ;; Exercise 2.32. We can represent a set as a list of distinct elements, and we can represent the set of all subsets of the set as a list of lists. For example, if the set is (1 2 3), then the set of all subsets is (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)). Complete the following definition of a procedure that generates the set of subsets of a set and give a clear explanation of why it works:
12 (define (subsets s)
13 (if (null? s)
14 '(())
15 (let ((rest (subsets (cdr s))))
16 (append rest
17 (map (lambda (subset)
18 (cons (car s) subset))
19 rest)))))
21 (test-case (subsets '(1 2 3)) '(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)))
23 ;; what we're doing here is breaking down the problem from originally trying
24 ;; to find all the subsets of S to instead finding the subsets
25 ;; of all but the first number
26 ;; we then take this and add it to all the subsets without the first number
27 ;; but now with the first number put in the front