Blob


1 (define (test-case actual expected)
2 (newline)
3 (display "Actual: ")
4 (display actual)
5 (newline)
6 (display "Expected: ")
7 (display expected)
8 (newline))
10 ;; Exercise 2.29. A binary mobile consists of two branches, a left branch and a right branch. Each branch is a rod of a certain length, from which hangs either a weight or another binary mobile. We can represent a binary mobile using compound data by constructing it from two branches (for example, using list):
12 ;; A branch is constructed from a length (which must be a number) together with a structure, which may be either a number (representing a simple weight) or another mobile:
14 ;; a. Write the corresponding selectors left-branch and right-branch, which return the branches of a mobile, and branch-length and branch-structure, which return the components of a branch.
16 ;; b. Using your selectors, define a procedure total-weight that returns the total weight of a mobile.
18 ;; c. A mobile is said to be balanced if the torque applied by its top-left branch is equal to that applied by its top-right branch (that is, if the length of the left rod multiplied by the weight hanging from that rod is equal to the corresponding product for the right side) and if each of the submobiles hanging off its branches is balanced. Design a predicate that tests whether a binary mobile is balanced.
20 (define (make-mobile left right)
21 (list left right))
22 (define (make-branch length structure)
23 (list length structure))
24 (define (left-branch mobile)
25 (car mobile))
26 (define (right-branch mobile)
27 (cadr mobile))
28 (define (branch-length branch)
29 (car branch))
30 (define (branch-structure branch)
31 (cadr branch))
32 (define (weight? mobile)
33 (not (pair? mobile)))
34 (define (total-weight mobile)
35 (if (weight? mobile)
36 mobile
37 (+ (total-weight (branch-structure (left-branch mobile)))
38 (total-weight (branch-structure (right-branch mobile))))))
40 (define m1 (make-mobile (make-branch 4 5) (make-branch 10 3)))
41 (define m2 (make-mobile (make-branch 2 3) (make-branch 3 4)))
42 (define m3 (make-mobile (make-branch 4 m1) (make-branch 5 m1)))
43 (define m4 (make-mobile (make-branch 7 m3) (make-branch 15 m2)))
44 (define m5 (make-mobile (make-branch 1 m4) (make-branch 3 m3)))
46 ;; (test-case (total-weight 4) 4)
47 ;; (test-case (total-weight m1) 8)
48 ;; (test-case (total-weight m2) 7)
49 ;; (test-case (total-weight m3) 16)
50 ;; (test-case (total-weight m4) 23)
51 ;; (test-case (total-weight m5) 39)
53 (define (balanced? mobile)
54 (if (weight? mobile)
55 #t
56 (let* ((lb (left-branch mobile))
57 (rb (right-branch mobile))
58 (ls (branch-structure lb))
59 (rs (branch-structure rb)))
60 (and
61 (balanced? ls)
62 (balanced? rs)
63 (= (* (branch-length lb)
64 (total-weight ls))
65 (* (branch-length rb)
66 (total-weight rs)))))))
68 (define m6 (make-mobile (make-branch 6 6) (make-branch 9 4))) ;; weight 10
69 (define m7 (make-mobile (make-branch 4 5) (make-branch 10 2))) ;; weight 7
70 (define m8 (make-mobile (make-branch 5 m6) (make-branch 2 25))) ;; wgt 35
71 (define m9 (make-mobile (make-branch 2 m8) (make-branch 10 m7))) ;; wgt 42
72 (define m10 (make-mobile (make-branch 1 m9) (make-branch 6 m7))) ;; wgt 49
74 (test-case (balanced? m1) #f)
75 (test-case (balanced? m2) #f)
76 (test-case (balanced? m3) #f)
77 (test-case (balanced? m4) #f)
78 (test-case (balanced? m5) #f)
79 (test-case (balanced? m6) #t)
80 (test-case (balanced? m7) #t)
81 (test-case (balanced? m8) #t)
82 (test-case (balanced? m9) #t)
83 (test-case (balanced? m10) #t)
85 ;; d. Suppose we change the representation of mobiles so that the constructors are
87 (define (make-mobile left right)
88 (cons left right))
89 (define (make-branch length structure)
90 (cons length structure))
92 ;; How much do you need to change your programs to convert to the new representation?
94 ;; Just need to change these 4:
96 (define (left-branch mobile)
97 (car mobile))
98 (define (right-branch mobile)
99 (cdr mobile))
100 (define (branch-length branch)
101 (car branch))
102 (define (branch-structure branch)
103 (cdr branch))