SICP

SICP を読んでみる #43 第二章 pp.63-65

問題回答

問2.27

(define (deep-reverse items)
  (define (iter items ret)
    (if (null? items)
        ret
        (iter (cdr items) (cons (reverse (car items)) ret))))

  (iter (cdr items) (cons (reverse (car items)) ())))

(define x (list (list 1 2) (list 3 4)))
(deep-reverse x)

問2.28

(define (fringe items)
  (define (iter items ret)
    (display items)(newline)
    (cond ((not (pair? items)) (append ret (list items)))
          ((null? (cdr items)) (append ret (iter (car items) ())))
          (else (append ret (iter (car items) ()) (iter (cdr items) ())))))

  (iter items ()))

(define x (list (list 1 2) (list 3 4)))
(fringe x)

問2.29
a.

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

(define (make-branch length structure)
  (list length structure))

(define (left-branch mobil)
  (car mobil))

(define (right-branch mobil)
  (cadr mobil))

(define (branch-length branch)
  (car branch))

(define (branch-structure branch)
  (cadr branch))


(define m (make-mobil (make-branch 1 ()) (make-branch 3 ())))
(left-branch m)
(right-branch m)

(branch-length (left-branch m))
(branch-structure (left-branch m))

b.

(define (total-weight mobil)
  (define (iter branch)
    (cond ((null? branch) 0)
          ((pair? branch) (+ (iter (left-branch branch)) (iter (right-branch branch))))
          (branch-structure branch)))

  (iter mobil))

(total-weight m)

c.

(define (balanced? mobil)
  (let ((lb (left-branch mobil))
        (rb (right-branch mobil)))
    (if (= (* (total-weight lb) (branch-length lb))
           (* (total-weight rb) (branch-length rb)))
        (begin (display "balanced")(newline))
        (begin (display "unbalanced")(newline)))))

(define m (make-mobil (make-branch 1 ()) (make-branch 3 ())))
(balanced? m)
(define m (make-mobil (make-branch 1 ()) (make-branch 1 ())))
(balanced? m)

d.
枝と、枝の部品の選択肢を修正すれば対応可

(define (left-branch mobil)
  (car mobil))

(define (right-branch mobil)
  (cdr mobil))

(define (branch-length branch)
  (car branch))

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

本文

木の写像
list に対するのと同様、再帰と一緒になった map を使用する

問題回答

問2.30

(define (square-tree items)
  (cond ((null? items) ())
        ((not (pair? items)) (* items items))
        (else (cons (square-tree (car items))
                    (square-tree (cdr items))))))

(square-tree (list 1
                   (list 2 (list 3 4) 5)
                   (list 6 7)))

map 版

(define (square-tree tree)
  (map (lambda (sub-tree)
         (if (pair? sub-tree)
             (square-tree sub-tree)
             (* sub-tree sub-tree)))
       tree))

(square-tree (list 1
                   (list 2 (list 3 4) 5)
                   (list 6 7)))

問2.31

(define (tree-map tree f)
  (map (lambda (sub-tree)
           (if (pair? sub-tree)
               (tree-map sub-tree f)
               (f sub-tree)))
       tree))

(define (square x) (* x x))

(define (square-tree tree)
  (tree-map tree square ))

(square-tree (list 1
                   (list 2 (list 3 4) 5)
                   (list 6 7)))

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です