Blob


1 (defun make-mobile (left right)
2 (list left right))
3 (defun left-branch (mobile)
4 (first mobile))
5 (defun right-branch (mobile)
6 (second mobile))
7 (defun make-branch (len structure)
8 (list len structure))
9 (defun branch-len (branch)
10 (first branch))
11 (defun branch-structure (branch)
12 (second branch))
14 (defun structure-is-weight? (structure)
15 (atom structure))
16 (defun weight-of-branch (branch)
17 (let ((struct (branch-structure branch)))
18 (if (structure-is-weight? struct)
19 struct
20 (weight-of-mobile struct))))
21 (defun weight-of-mobile (mobile)
22 (+ (weight-of-branch (left-branch mobile))
23 (weight-of-branch (right-branch mobile))))
24 (defun torque-of-branch (branch)
25 (* (branch-len branch)
26 (weight-of-branch branch)))
27 (defun branch-balanced? (branch)
28 "A branch is balanced either when it has a structure
29 that's a simple weight, or when the structure is
30 a balanced mobile"
31 (let ((struct (branch-structure branch)))
32 (or
33 (structure-is-weight? struct)
34 (mobile-balanced? struct))))
35 (defun mobile-balanced? (mobile)
36 (let ((lb (left-branch mobile))
37 (rb (right-branch mobile)))
38 (and
39 (= (torque-of-branch lb)
40 (torque-of-branch rb))
41 (branch-balanced? lb)
42 (branch-balanced? rb))))