(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 mobile)) (define (branch-length branch) (car branch)) (define (branch-structure branch) (cadr branch)) (define (total-weight mobile) (if (number? mobile) ; mobile is a single weight mobile (+ (total-weight (branch-structure (left-branch mobile))) (total-weight (branch-structure (right-branch mobile)))))) (define (balanced? mobile) (define (branch-torque branch) (* (branch-length branch) (total-weight (branch-structure branch)))) (if (number? mobile) #t (= (branch-torque (left-branch mobile)) (branch-torque (right-branch mobile))))) ; 1 ]=> (define m (make-mobile (make-branch 2 (make-mobile (make-branch 2 1) (make-branch 1 2))) (make-branch 3 2))) ; ; ;Value: m ; ; 1 ]=> (balanced? m) ; ; ;Value: #t ; ; 1 ]=> (balanced? (make-mobile (make-branch 6 7) (make-branch 6 8))) ; ; ;Value: #f ; ; If the constructors change to: ; (define (make-mobile left right) ; (cons left right)) ; ; (define (make-branch length structure) ; (cons length structure)) ; ; then only the folloing selectors have to change ; (define (right-branch mobile) ; (cdr mobile)) ; ; (define (branch-structure branch) ; (cdr branch))