-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2.29.scm
44 lines (33 loc) · 1.13 KB
/
2.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
(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 (terminal-branch? branch)
(not (list? (car branch))))
(define x '(((1 2) (3 4)) ((5 6) (7 8))))
(define (total-weight mobile)
(cond ((null? mobile) 0)
((terminal-branch? mobile)
(display (cdr mobile)) (branch-structure mobile))
(else
(+ (total-weight (left-branch mobile))
(total-weight (right-branch mobile))))))
(define (branch-torque branch)
(* (branch-length branch) (branch-structure branch)))
(define (mobile-torque mobile)
(cond ((null? mobile) 0)
((terminal-branch? mobile) (branch-torque mobile))
(else
(+ (mobile-torque (left-branch mobile))
(mobile-torque (right-branch mobile))))))
(define (balanced-mobile? mobile)
(equal? (mobile-torque (left-branch mobile))
(mobile-torque (right-branch mobile))))