From: Ramakrishnan Muthukrishnan Date: Thu, 22 Jul 2010 19:52:24 +0000 (+0530) Subject: solution to 2.32. One of the best exercises in this section. X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/css/flags/about.html?a=commitdiff_plain;h=031d46652252d4843dffebaa8cd69b9f1f6bb924;p=sicp.git solution to 2.32. One of the best exercises in this section. --- diff --git a/src/sicp/ex2_32.clj b/src/sicp/ex2_32.clj new file mode 100644 index 0000000..7f0a279 --- /dev/null +++ b/src/sicp/ex2_32.clj @@ -0,0 +1,25 @@ +(ns sicp.ex2_32 + (:use [clojure.test])) + +(defn subsets [s] + (if (empty? s) + (list ()) + (let [r (subsets (rest s))] + (concat r (map #(cons (first s) %) r))))) + +(comment +" +Let s be '(1 2 3). + +1. (subset '(1 2 3)) => (subset '(2 3)) => (subset '(3)) => (subset '()) +2. At this point subset returns. +3. r becomes (). value of s is '(3). +4. concat () with result of (map #(cons 3 %) '()) => (() (3)) +5. returns to previous call, where s is '(2 3). +6. (map #(cons 2 %) '(() (3))) => ((2) (2 3)) +7. concat the above with previous r => (() (3) (2) (2 3)) +8. return to the previous call, where s is '(1 2 3) +9. (map #(cons 1 %) '(() (3) (2) (2 3))) => ((1) (1 3) (1 2) (1 2 3)) +10. Append the above with previous value of r => (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)) +" +) \ No newline at end of file