my attempt to do the exercises in sicp.

Wednesday, July 16, 2008

sicp exercise 2.29


; 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.

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

; 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.

; 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?


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

; a.

(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 my-mobile
  (make-mobile
    (make-branch 10
                 (make-mobile
                     (make-branch 10 2)
                     (make-branch 2 100)))
    (make-branch 10 23)))

(display my-mobile) (newline)

; b.

(define (weight-mobile mobile)
  (+
    (weight-branch (left-branch mobile))
    (weight-branch (right-branch mobile))))

(define (weight-branch branch)
  (let ((weight (branch-structure branch)))
  (cond ((not (pair? weight)) weight)
        (else (weight-mobile weight)))))

(display (weight-mobile my-mobile)) (newline)

; c.

(define (torque branch)
  (* (branch-length branch) (weight-branch branch)))

(define (mobile-balanced? mobile)
  (or (not (pair? mobile))
      (and (= (torque (left-branch mobile)) (torque (right-branch mobile)))
           (mobile-balanced? (branch-structure (left-branch mobile)))
           (mobile-balanced? (branch-structure (right-branch mobile))))))

(display (mobile-balanced? my-mobile)) (newline)

(define my-balanced-mobile
  (make-mobile
    (make-branch 10
                 (make-mobile
                     (make-branch 20 5)
                     (make-branch 2 50)))
    (make-branch 55 10)))

(display (mobile-balanced? my-balanced-mobile)) (newline)

; d.

; If the representation is changed, only the selectors need to be changed.

No comments: