Implement up to 2.32
parent
ef696e003f
commit
50cdf446fe
|
@ -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)))
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
(load "util.scm")
|
||||
|
||||
|
||||
(display "\nex-2.34\n")
|
||||
|
||||
(display "\nex-2.35\n")
|
||||
|
||||
(display "\nex-2.36\n")
|
Loading…
Reference in New Issue