2008. 4. 18.

연습문제 2.29

; a
(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)))

;b
(define (is-weight? structure)
  (not (pair? structure)))

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

(define (weight-of-branch branch)
  (let ((struct (branch-structure branch)))
    (cond ((is-weight? struct) struct)
          (else
           (weight-of-mobile branch)))))

;c
(define (balanced-mobile? mobile)
  (= (weight-of-mobile (left-branch mobile))
     (weight-of-mobile (right-branch mobile))))

;d
주요한 접근자로 left-branch, right-branch, branch-length, branch-structure, is-weight? 를 변경해야한다.

댓글 없음:

댓글 쓰기