Exercise 2.29 of SICP

Exercise 2.29: A binary mobile consists of two branches, a left branch and a right branch. Each branch is a rod of a certain length, from which hangs either a weight or another binary mobile. We can represent a binary mobile using compound data by constructing it from two branches (for example, using list):

(define (make-mobile left right)
  (list left right))

A branch is constructed from a length (which must be a number) together with a structure, which may be either a number (representing a simple weight) or another mobile:

(define (make-branch length structure)
  (list length structure))

a. Write the corresponding selectors left-branch and right-branch, which return the branches of a mobile, and branch-length and branch-structure, which return the components of a branch.

(define (make-mobile left right)
  (list left right))
(define (left-branch mobile)
  (if (null? mobile) 
    '()
    (car mobile)))
(define (right-branch mobile)
  (if (null? mobile) 
    '()
    (car (cdr mobile))))
(define (make-branch length structure)
  (list length structure))
(define (branch-length branch)
  (if (null? branch)
    0
    (car branch)))
(define (branch-structure branch)
  (if (null? branch)
    0
    (car (cdr branch))))

b. Using your selectors, define a procedure total-weight that returns the total weight of a mobile.

(define (total-weight mobile)
  (define (weight? x)
    (not (pair? x)))
  (define (weight-of-branch branch)
    (let ((struct (branch-structure branch)))
      (if (weight? struct)
        struct
        (total-weight struct))))

  (let ((lbranch (left-branch mobile))
        (rbranch (right-branch mobile)))
    (cond ((null? mobile) 0)
          (else
            (+ 
              (weight-of-branch lbranch)
              (weight-of-branch rbranch))))))
> (define m (make-mobile (make-branch 1 2) (make-branch 3 4))) 
> (total-weight m)
6
> (define n (make-mobile (make-branch 1 (make-mobile (make-branch 1 2) (make-bra nch 3 4))) (make-branch 2 (make-mobile (make-branch 2 2) (make-branch 3 1))))) 
> (total-weight n)
9 

c. A mobile is said to be balanced if the torque applied by its top-left branch is equal to that applied by its top-right branch (that is, if the length of the left rod multiplied by the weight hanging from that rod is equal to the corresponding product for the right side) and if each of the submobiles hanging off its branches is balanced. Design a predicate that tests whether a binary mobile is balanced.

(define (torque-balanced? mobile)
  (define (weight? x)
    (not (pair? x)))
  (define (torque branch)
    (let ((struct (branch-structure branch)))
      (* (branch-length branch)
         (if (weight? struct)
           struct
           (+ (torque (left-branch struct))
              (torque (right-branch struct)))))))
  (if (null? mobile) 
    #t
    (= (torque (left-branch mobile))
       (torque (right-branch mobile)))))
> (define m (make-mobile (make-branch 1 2) (make-branch 2 1))) 
> (torque-balanced? m)
#t 
> (torque-balanced? n)
#t 
> (define m (make-mobile (make-branch 1 2) (make-branch 2 2))) 
> (torque-balanced? m)
#f

d. Suppose we change the representation of mobiles so that the constructors are

(define (make-mobile left right)
  (cons left right))
(define (make-branch length structure)
  (cons length structure))

How much do you need to change your programs to convert to the new representation?

Only two changes are needed to selectors of the right side elements because (cdr (list 1 (list 2))) returns ((2)). However the nested list problem doesn’t happen with dotted pairs.

(define (right-branch mobile)
  (if (null? mobile) 
    '()
    (cdr mobile)))
(define (branch-structure branch)
  (if (null? branch)
    0
    (cdr branch)))