Exercise2-28 <---> Exercise2-30

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))


Scheme solution:

  1. 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 (left-branch mobile) (car mobile))
    (define (right-branch mobile) (cadr mobile))
    (define (branch-length branch) (car branch))
    (define (branch-structure branch) (cadr branch)) 
    
  2. Using your selectors, define a procedure total-weight that returns the total weight of a mobile.
    (define (branch-weight branch)
      (if (not (pair? (branch-structure branch)))
          (branch-structure branch)
          (total-weight (branch-structure branch))))
    (define (total-weight mobile)
      (+ (branch-weight (left-branch mobile))
         (branch-weight (right-branch mobile))))
    
  3. 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 (balanced? mobile)
      (define (has-sub? branch) (pair? (branch-structure branch)))
      (let ((left (left-branch mobile))
            (right (right-branch mobile)))
        (and (if (has-sub? left)
                 (balanced? (branch-structure left)))
             (if (has-sub? right)
                 (balanced? (branch-structure right)))
             (= (* (branch-length left) (branch-weight left))
                (* (branch-length right) (branch-weight right))))))
    
  4. 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 (right-branch mobile) (cdr mobile))
    (define (branch-structure branch) (cdr branch))
    

Exercise2-28 <---> Exercise2-30


Comments

My variant of c:

(define (balanced? mobile)
  (define (balanced-mob? mob)
    (= (* (branch-length (left-branch mob))
             (total-weight (branch-structure (left-branch mob))))
        (* (branch-length (right-branch mob))
             (total-weight (branch-structure (right-branch mob))))))
  (if (not (pair? mobile))
      (equal?)
      (and (balanced-mob? mobile)
              (balanced? (branch-structure (left-branch mobile)))
              (balanced? (branch-structure (right-branch mobile))))))
Posted by tymmym at 2008-05-28 10:43:33 X
Variant of b and c

;b.
(define (is-leaf-branch? branch)
  (if (and (not (null? (branch-structure branch))) (not (pair? (branch-structure branch))))
      #t
      #f))

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

(define (branch-weight branch)
  (cond ((is-leaf-branch? branch) (branch-structure branch))
      (else (total-weight (branch-structure branch)))))

;c.
(define (torque branch)
  (* (branch-weight branch) (branch-length branch)))

(define (balanced? mobile)
  (= (torque (left-branch mobile)) (torque (right-branch mobile))))
Posted by SriramDevadas at 2008-06-05 18:47:59
My version of b:
(define (branch-weight branch)
  (cond ((number? (branch-structure branch))
         (branch-structure branch))
        (else (total-weight (branch-structure branch)))))

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

And of c:

(define (mobile-balanced? mobile)
  (and (= (left-momentum mobile)
          (right-momentum mobile))
       (branch-balanced? (right-branch mobile))))

(define (branch-balanced? branch)
  (cond ((number? (branch-structure branch)))
        (else (mobile-balanced? (branch-structure branch)))))

(define (left-momentum mobile)
  (* (branch-length (left-branch mobile))
     (branch-weight (left-branch mobile))))

(define (right-momentum mobile)
  (* (branch-length (right-branch mobile))
     (branch-weight (right-branch mobile))))
Posted by Penguino at 2008-08-22 18:12:51

how to represent an empty branch? '() or ‘(0 0)??

Posted by lenatis at 2008-09-05 04:38:56

This was my solution for Problem (c). Is it correct? Please let me know. Thanks :)

(define (balanced? branch)

  • (if (not (pair? (branch-structure branch))) branch
    • (balanced-mobile? (branch-structure branch))))

(define (balanced-mobile? mobile)

  • (if (equal? (* (total-weight (balanced? (left-branch mobile))) (branch-length (left-branch mobile)))
    • (* (total-weight (balanced? (right-branch mobile))) (branch-length (right-branch mobile)))) #t
      • #f))
Posted by student :) at 2008-10-31 09:25:22 X

   1 ;my answer (nagoya)
   2 (define (total-weight mobile)
   3   (cond ((and (not (pair? mobile))
   4               (not (null? mobile)))
   5          0)
   6         ((not (pair? (branch-weight mobile))) (branch-weight mobile))
   7         (else (+ (total-weight (left-branch mobile)) 
   8                  (total-weight (right-branch mobile))))))
Posted by 91 at 2008-11-18 03:19:28 X

:) :)) :( ;) :\ |) X-( B) Markup

Exercise2-29 (last edited 2008-05-11 11:36:21 by localhost)