SICP solution exercise 2.29

Solution for exercise 2.29 of SICP:

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

(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, 
(define (left-branch mobile)
  (car mobile))
(define (right-branch mobile)
  (car (cdr mobile)))

; * branch-length and
; * branch-structure,
;   which return the components of a branch.
(define (branch-length branch)
  (car branch))

(define (branch-structure branch)
  (car (cdr branch)))

;;;; tests
(define b5
  (make-branch 10 6))

(define b6
  (make-branch 10 7))

(define m3
  (make-mobile b5 b6))

(define b3
  (make-branch 10 m3))

(define b4
  (make-branch 10 5))

(define m2
  (make-mobile b3 b4))

(define b1
  (make-branch 10 3))

(define b2
  (make-branch 10 m2))

(define m1
  (make-mobile b1 b2))

;b.  Using your selectors, define a procedure total-weight 
;    that returns the total weight of a mobile.
(define (total-weight structure)
  (if (pair? structure)
      (+ (total-weight (branch-structure (left-branch structure)))
         (total-weight (branch-structure (right-branch structure))))
      structure))

;;;; tests
(total-weight m1)
(total-weight m2)
(total-weight m3)

;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 branch)
  (* (branch-length branch)
     (total-weight (branch-structure branch))))

;;;; tests
(torque b1)
(torque b2)

(define (balanced? structure)
  (if (pair? structure)
      (and (equal? (torque (left-branch structure))
                   (torque (right-branch structure)))
           (balanced? (branch-structure (left-branch structure)))
           (balanced? (branch-structure (right-branch structure))))
       #t))
           
(define m4
  (make-mobile 
   (make-branch 1 30)
   (make-branch 2 (make-mobile 
                   (make-branch 3 5)
                   (make-branch 2 10)))))

m4
(balanced? m4)

(define m5
  (make-mobile
   (make-branch 2 (make-mobile
                   (make-branch 6 5)
                   (make-branch 3 10)))
   (make-branch 3 (make-mobile
                   (make-branch 2 5)
                   (make-branch 2 5)))))

m5
(balanced? m5)

;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?
;Answer: the implementation of right-branch and branch-structure must change

This entry was posted in SICP and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>