-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2_29.rkt
46 lines (33 loc) · 1.11 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
#lang racket
#| Solution for exercise 2_29. |#
(require rackunit "../solutions/utils.rkt")
(provide make-mobile make-branch total-weight balanced?)
(define (make-mobile left-branch right-branch)
(cons left-branch right-branch))
(define (make-branch len structure)
(cons len structure))
(define (mobile? structure) (pair? structure))
(define (left-branch mobile) (car mobile))
(define (right-branch mobile) (cdr mobile))
(define (len branch) (car branch))
(define (structure branch) (cdr branch))
(define (total-weight mobile)
(+ (branch-weight (left-branch mobile))
(branch-weight (right-branch mobile))))
(define (branch-weight branch)
(let ((struct (structure branch)))
(if (mobile? struct)
(total-weight struct)
struct)))
(define (balanced? mobile)
(define (branch-moment branch)
(* (len branch) (branch-weight branch)))
(if (not (mobile? mobile))
true
(let ((lb (left-branch mobile))
(rb (right-branch mobile)))
(and
(= (branch-moment lb)
(branch-moment rb))
(and (balanced? (structure lb))
(balanced? (structure rb)))))))