-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.29.rkt
59 lines (46 loc) · 1.65 KB
/
2.29.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
#lang sicp
(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)
; cadr because it's a list, not a pair
(car (cdr mobile)))
(define (branch-length branch)
(car branch))
(define (branch-structure branch)
; cadr because it's a list, not a pair
(car (cdr branch)))
(define (is-weight? branch)
(number? branch))
(define (total-weight mobile)
(let ((left (branch-structure (left-branch mobile)))
(right (branch-structure (right-branch mobile))))
(+ (if (is-weight? left)
left
(total-weight left))
(if (is-weight? right)
right
(total-weight right)))))
(define (branch-torque branch)
(if (is-weight? (branch-structure branch))
(* (branch-structure branch) (branch-length branch))
(* (total-weight (branch-structure branch)) (branch-length branch))))
(define (mobile-is-balanced mobile)
(let ((left (branch-structure (left-branch mobile)))
(right (branch-structure (right-branch mobile))))
(and (= (branch-torque (left-branch mobile))
(branch-torque (right-branch mobile)))
(if (is-weight? left)
#t
(mobile-is-balanced left))
(if (is-weight? right)
#t
(mobile-is-balanced right)))))
(define mob (make-mobile (make-branch 1 (make-mobile (make-branch 1 20) (make-branch 1 30))) (make-branch 1 40)))
(define mob1 (make-mobile (make-branch 1 (make-mobile (make-branch 1 20) (make-branch 1 20))) (make-branch 1 40)))
(total-weight mob)
(mobile-is-balanced mob)
(mobile-is-balanced mob1)