diff --git a/ex-2_18-xx.scm b/ex-2_18-32.scm similarity index 71% rename from ex-2_18-xx.scm rename to ex-2_18-32.scm index 07aa218..8c55ddd 100644 --- a/ex-2_18-xx.scm +++ b/ex-2_18-32.scm @@ -1,3 +1,5 @@ +(load "util.scm") + (display "\nex-2.24\n") (define x (cons (list 1 2) (list 3 4))) @@ -152,5 +154,53 @@ (define (make-branch length structure) (cons length structure)) -(display "\nex-2.30") +(display "\nex-2.30 - tree square\n") + +; using tail recursion +(define (square-tree t) + (cond + ((null? t) nil) + ((not (pair? t)) (square t)) + (else (cons (square-tree (car t)) (square-tree (cdr t)))))) + +(display x) (newline) +(display (square-tree x)) (newline) + +; using map +(define (square-tree t) + (cond + ((null? t) nil) + ((not (pair? t)) (square t)) + (else (map square-tree t)))) + +(display (square-tree x)) (newline) + +(display "\nex-2.31\n") + +(define (tree-map proc tree) + (cond + ((null? tree) tree) + ((not (pair? tree)) (proc tree)) + (else (map (lambda (t) tree-map proc t) tree)))) + +(define (square-tree tree) (tree-map square tree)) +(display (square-tree x)) (newline) + +(display "\nex-2.32\n") + +(define (subsets s) + (if (null? s) + (list nil) + (let ((rest (subsets (cdr s)))) + (append rest (map (lambda (r) (cons (car s) r)) rest))))) + +; Assuming we have an oracle procedure subsets and we get a new list that we +; split into car and cdr). If we use the oracle to compute the subsets for cdr +; then we get a new list rest. To compute the new subsets from that list we +; have to a) keep the rest as it is and b) add the current element (car) to all +; the subsets (rest). The only other tricky part (that was already given) is +; that when we get an empty list we want to return a list including that empty +; list. + +(display (subsets (list 1 2 3))) diff --git a/ex-2_33-xx.scm b/ex-2_33-xx.scm new file mode 100644 index 0000000..5e2ad75 --- /dev/null +++ b/ex-2_33-xx.scm @@ -0,0 +1,8 @@ +(load "util.scm") + + +(display "\nex-2.34\n") + +(display "\nex-2.35\n") + +(display "\nex-2.36\n")