Code Review
Compare your solutions
#| BEGIN (Write your solution here) |#
(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)
(car (cdr mobile)))
(define (branch-length branch)
(car branch))
(define (branch-structure branch)
(car (cdr branch)))
(define (total-weight mobile)
(if (not (pair? mobile))
mobile
(+ (total-weight (branch-structure (left-branch mobile)))
(total-weight (branch-structure (right-branch mobile))))))
(define (mobile-balanced? mobile)
(if (not (pair? mobile))
#t
(let ((left-torque (* (branch-length (left-branch mobile))
(total-weight (branch-structure (left-branch mobile)))))
(right-torque (* (branch-length (right-branch mobile))
(total-weight (branch-structure (right-branch mobile))))))
(and (= left-torque right-torque)
(mobile-balanced? (branch-structure (left-branch mobile)))
(mobile-balanced? (branch-structure (right-branch mobile)))))))
#| END |#