Chat (Lingr.com)
Informaiton
Daily
Column
- MySQL日本語の旅(5/1)
- アクセス向上秘伝(5/9)
- 一風変ったHaskellλ門(6/13)
- SICP Answer Book (5/31) 問題3.26追加
Zope Solution
Extra
アーカイブ
OSS案内所
Site Info
関連リンク
##(link2sicp "book-Z-H-15.html#%_thm_2.29" "Exercise 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) (cadr branch))
b.
(define (total-weight mobile)
(let ((left (left-branch mobile))
(right (right-branch mobile)))
(+ (branch-weight left)
(branch-weight right))))
(define (branch-weight branch)
(let ((structure (branch-structure branch)))
(if (not (pair? structure))
structure
(total-weight structure))))
c.
(define (balanced? mobile)
(if (balanced-mobile? mobile) #t #f))
(define (balanced-mobile? mobile)
(let ((left (left-branch mobile))
(right (right-branch mobile)))
(let ((left-weight (balanced-branch? left)))
(and left-weight
(let ((right-weight (balanced-branch? right)))
(and right-weight
(= (* (branch-length left) left-weight)
(* (branch-length right) right-weight))
(+ left-weight right-weight)))))))
(define (balanced-branch? branch)
(let ((structure (branch-structure branch)))
(if (pair? structure)
(balanced-mobile? structure)
structure)))
d. 選択子 right-branch、branch-structure を変更するだけでよい。
コード
##(sicp-answer-code "ex-2.29.scm")