Permalink
Browse files

Add 2_32

  • Loading branch information...
1 parent 75d0fe3 commit 3dbd69a1f9d4b77f9a5a37c994ff7497b14cbdb3 @fastred committed Dec 29, 2012
Showing with 24 additions and 0 deletions.
  1. +24 −0 2_32.scm
View
@@ -0,0 +1,24 @@
+(define (subsets s)
+ (if (null? s)
+ (list '())
+ (let ((rest (subsets (cdr s))))
+ (append rest (map
+ (lambda (x) (cons (car s) x))
+ rest)))))
+
+(subsets (list 1 2 3))
+
+;I did this one with pen & paper. My way of reasoning:
+;'rest' is a list of subsets created from the cdr of the list 's'.
+;Those subsets either include or don't include car of 's'.
+;That's why we 'append':
+; - 'rest',
+; - list of consed cars with every element of 'rest'.
+
+;It's easy to picture it if we trace recursion backwards:
+;(rest '()) => '(())
+;(rest '(3)) => '(() (3))
+;(rest '(2 3)) => '(() (3) (2) (2 3))
+;(rest '(1 2 3)) => '(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))
+
+;Note: '=>' means "returns"

0 comments on commit 3dbd69a

Please sign in to comment.