-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection-2.2.2.rkt
102 lines (75 loc) · 2.31 KB
/
section-2.2.2.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#lang sicp
; ex 2.25
; cadaddr
; caar
; ?
; ex 2.26
; '(1 2 3 4 5 6)
; '((1 2 3) 4 5 6))
; '((1 2 3) (4 5 6))
; ex 2.27
(define (deep-reverse xs)
(define (loop xs0 accu)
(cond
((null? xs0) accu)
((list? (car xs0)) (loop (cdr xs0) (cons (deep-reverse (car xs0)) accu)))
(else (loop (cdr xs0) (cons (car xs0) accu)))))
(loop xs '()))
; ex 2.28
(define (fringe xs)
(cond
((null? xs) '())
((pair? (car xs)) (append (fringe (car xs)) (fringe (cdr xs))))
(else (cons (car xs) (fringe (cdr xs))))))
; ex 2.29
(define (make-mobile left right)
(list left right))
(define (make-branch len structure)
(list len structure))
(define (left-branch mobile)
(car mobile))
(define (right-branch mobile)
(cadr mobile))
(define (branch-length branch)
(car branch))
(define (branch-struct branch)
(cadr branch))
(define (total-weight mobile)
(+ (branch-weight (left-branch mobile))
(branch-weight (right-branch mobile))))
(define (branch-weight branch)
(cond
[(not (list? (branch-struct branch))) (branch-struct branch)]
[else (total-weight (branch-struct branch))]))
(define (torque branch)
(* (branch-length branch) (branch-weight branch)))
(define (balanced? mobile)
(if (not (list? mobile))
#t
(and (= (torque (left-branch mobile)) (torque (right-branch mobile)))
(balanced? (branch-struct (left-branch mobile)))
(balanced? (branch-struct (right-branch mobile))))))
; ex 2.30
(define (square-tree tree)
(cond
[(null? tree) '()]
[(not (pair? tree)) (* tree tree)]
[else (cons (square-tree (car tree))
(square-tree (cdr tree)))]))
(define (square-tree-map tree)
(map (lambda (x) (if (pair? x) (square-tree-map x) (* x x)))
tree))
; ex 2.31
(define (tree-map proc tree)
(map (lambda (x) (if (pair? x) (tree-map proc x) (proc x)))
tree))
; ex 2.32
(define (subsets s)
(if (null? s)
(list '())
(let ((rest (subsets (cdr s))))
(append rest (map (lambda (x) (cons (car s) x)) rest)))))
; If subsets is a procedure for computing all subsets of a set s, then the
; result of (subsets (cdr s)) is all subsets of the set s excluding the first
; element, thus the all subsets of s is (subsets (cdr s)) plus the result of
; consing the first element of s onto every list in (subsets (cdr s)).