-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-29.rkt
52 lines (38 loc) · 1.25 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
#lang racket
(define not-pair? (negate pair?))
(define (make-mobile left right)
(list left right))
(define (make-branch length structure)
(list length structure))
(define (left-branch bm)
(car bm))
(define (right-branch bm)
(cadr bm))
(define (branch-length br)
(car br))
(define (branch-structure br)
(cadr br))
(define (total-weight bm)
(cond [(not-pair? bm) bm]
[(not-pair? (right-branch bm)) (right-branch bm)]
[(not-pair? (left-branch bm)) (total-weight (right-branch bm))]
[else (+ (total-weight (left-branch bm))
(total-weight (right-branch bm)))]))
(define x (make-mobile (make-branch 4 10) (make-branch 4 10)))
(define y (make-mobile (make-branch 4 10) x))
(total-weight x)
(total-weight y)
(define z (make-mobile (make-branch 2 3) (make-branch 2 3)))
(total-weight z)
(define (balanced? bm)
(define (torque b)
(* (branch-length b) (total-weight (branch-structure b))))
(if (not-pair? bm)
true
(and (= (torque (left-branch bm))
(torque (right-branch bm)))
(balanced? (branch-structure (left-branch bm)))
(balanced? (branch-structure (right-branch bm))))))
(balanced? x)
(define d (make-mobile (make-branch 10 z) (make-branch 12 5)))
(balanced? d)