-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path2.29.scm
executable file
·51 lines (42 loc) · 1.37 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
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env chicken-scheme
(use test)
(define (make-mobile left right)
(list left right))
(define (make-branch length structure)
(list length structure))
(define left-branch car)
(define right-branch cadr)
(define branch-length car)
(define branch-structure cadr)
(define (total-weight mobile)
(let ((left-structure (branch-structure (left-branch mobile)))
(right-structure (branch-structure (right-branch mobile))))
(+ (if (number? left-structure)
left-structure
(total-weight left-structure))
(if (number? right-structure)
right-structure
(total-weight right-structure)))))
(define mobile
(make-mobile (make-branch
2
(make-mobile
(make-branch
2
(make-mobile
(make-branch 3 16)
(make-branch 2 20)))
(make-branch 4 20)))
(make-branch 5 102.4)))
(define (torque branch)
(let ((structure (branch-structure branch)))
(* (branch-length branch)
(if (number? structure)
structure
(+ (torque (left-branch structure))
(torque (right-branch structure)))))))
(define (balanced? mobile)
(= (torque (left-branch mobile))
(torque (right-branch mobile))))
(test 158.4 (total-weight mobile))
(test-assert (balanced? mobile))