From 031d46652252d4843dffebaa8cd69b9f1f6bb924 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Fri, 23 Jul 2010 01:22:24 +0530 Subject: [PATCH] solution to 2.32. One of the best exercises in this section. --- src/sicp/ex2_32.clj | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/sicp/ex2_32.clj 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 -- 2.45.2