-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEx2_29.scm
54 lines (41 loc) · 1.37 KB
/
Ex2_29.scm
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
(define (make-mobile left right)
(list left right))
(define (make-branch length structure)
(list length structure))
(define (left-branch mobile)
(car mobile))
(define (right-branch mobile)
(car (cdr mobile)))
(define (branch-length branch)
(car branch))
(define (branch-structure branch)
(car (cdr branch)))
(define (branch-weight branch)
(let ((s (branch-structure branch)))
(if (pair? s)
(total-weight s)
s)))
(define (total-weight mobile)
(+ (branch-weight (left-branch mobile))
(branch-weight (right-branch mobile))))
(define (if-balance mobile)
(define (torque branch)
(* (branch-length branch) (branch-weight branch)))
(let ((left (left-branch mobile))
(right (right-branch mobile))
(s-left (branch-structure (left-branch mobile)))
(s-right (branch-structure (right-branch mobile))))
(if (= (torque left) (torque right))
(cond ((and (pair? s-left) (pair? s-right))
(and (if-balance s-left) (if-balance s-right)))
((pair? s-left) (if-balance s-left))
((pair? s-right) (if-balance s-right))
(else #t))
#f)))
(define level-1-mobile (make-mobile (make-branch 2 1)
(make-branch 1 2)))
(define level-2-mobile (make-mobile (make-branch 3 level-1-mobile)
(make-branch 9 1)))
(define level-3-mobile (make-mobile (make-branch 4 level-2-mobile)
(make-branch 8 2)))
(if-balance level-3-mobile)