1 665c255d 2023-08-04 jrmu (define (test-case actual expected)
3 665c255d 2023-08-04 jrmu (display "Actual: ")
4 665c255d 2023-08-04 jrmu (display actual)
6 665c255d 2023-08-04 jrmu (display "Expected: ")
7 665c255d 2023-08-04 jrmu (display expected)
10 665c255d 2023-08-04 jrmu ;; 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 665c255d 2023-08-04 jrmu (define (subsets s)
13 665c255d 2023-08-04 jrmu (if (null? s)
15 665c255d 2023-08-04 jrmu (let ((rest (subsets (cdr s))))
16 665c255d 2023-08-04 jrmu (append rest
17 665c255d 2023-08-04 jrmu (map (lambda (subset)
18 665c255d 2023-08-04 jrmu (cons (car s) subset))
21 665c255d 2023-08-04 jrmu (test-case (subsets '(1 2 3)) '(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)))
23 665c255d 2023-08-04 jrmu ;; what we're doing here is breaking down the problem from originally trying
24 665c255d 2023-08-04 jrmu ;; to find all the subsets of S to instead finding the subsets
25 665c255d 2023-08-04 jrmu ;; of all but the first number
26 665c255d 2023-08-04 jrmu ;; we then take this and add it to all the subsets without the first number
27 665c255d 2023-08-04 jrmu ;; but now with the first number put in the front