読者です 読者をやめる 読者になる 読者になる

SICPゼミ第34回

2章に帰ってきました。

put, get など

(define (key-compare a b) 0)
(define (make-table)
  (let (( local-table (list '*table* )))
    (define (lookup key-1 key-2)
      (let (( subtable
              (assoc key-1 (cdr local-table ))))
        (if subtable
            (let (( record
                    (assoc key-2 (cdr subtable ))))
              (if record (cdr record) false ))
            false )))
    (define (insert! key-1 key-2 value)
      (let (( subtable
              (assoc key-1 (cdr local-table ))))
        (if subtable
            (let (( record
                    (assoc key-2 (cdr subtable ))))
              (if record
                  (set-cdr! record value)
                  (set-cdr! subtable
                            (cons (cons key-2 value)
                                  (cdr subtable )))))
            (set-cdr! local-table
                      (cons (list key-1 (cons key-2 value ))
                            (cdr local-table )))))
      'ok)
    (define (dispatch m)
      (cond ((eq? m 'lookup-proc) lookup)
            ((eq? m 'insert-proc!) insert!)
            (else (error "Unknown operation: TABLE" m))))
    dispatch ))

(define operation-table (make-table ))
(define get (operation-table 'lookup-proc ))
(define put (operation-table 'insert-proc! ))

(define (attach-tag type-tag contents)
  (cons type-tag contents ))

(define (type-tag datum)
  (if (pair? datum)
      (car datum)
      (error "Bad tagged datum: TYPE-TAG" datum )))

(define (contents datum)
  (if (pair? datum)
      (cdr datum)
      (error "Bad tagged datum: CONTENTS" datum )))


(define (apply-generic op . args)
  (let (( type-tags (map type-tag args )))
    (let ((proc (get op type-tags )))
      (if proc
          (apply proc (map contents args))
          (error
           "No method for these types: APPLY-GENERIC"
           (list op type-tags ))))))

(define (add x y) (apply-generic 'add x y))
(define (sub x y) (apply-generic 'sub x y))
(define (mul x y) (apply-generic 'mul x y))
(define (div x y) (apply-generic 'div x y))

(define (install-scheme-number-package)
  (define (tag x) (attach-tag 'scheme-number x))
  (put 'add '(scheme-number scheme-number)
       (lambda (x y) (tag (+ x y))))
  (put 'sub '(scheme-number scheme-number)
       (lambda (x y) (tag (- x y))))
  (put 'mul '(scheme-number scheme-number)
       (lambda (x y) (tag (* x y))))
  (put 'div '(scheme-number scheme-number)
       (lambda (x y) (tag (/ x y))))
  (put 'make 'scheme-number (lambda (x) (tag x)))
  'done)

(define (install-rational-package)
  ;; 内部手続き
  (define (numer x) (car x))
  (define (denom x) (cdr x))
  (define (make-rat n d)
    (let ((g (gcd n d)))
      (cons (/ n g) (/ d g))))
  (define (add-rat x y)
    (make-rat (+ (* (numer x) (denom y))
                 (* (numer y) (denom x)))
              (* (denom x) (denom y))))
  (define (sub-rat x y)
    (make-rat (- (* (numer x) (denom y))
                 (* (numer y) (denom x)))
              (* (denom x) (denom y))))
  (define (mul-rat x y)
    (make-rat (* (numer x) (numer y))
              (* (denom x) (denom y))))
  (define (div-rat x y)
    (make-rat (* (numer x) (denom y))
              (* (denom x) (numer y))))
  ;; システムのほかの部分とのインターフェイス
  (define (tag x) (attach-tag 'rational x))
  (put 'add '(rational rational)
       (lambda (x y) (tag (add-rat x y))))
  (put 'sub '(rational rational)
       (lambda (x y) (tag (sub-rat x y))))
  (put 'mul '(rational rational)
       (lambda (x y) (tag (mul-rat x y))))
  (put 'div '(rational rational)
       (lambda (x y) (tag (div-rat x y))))
  (put 'make 'rational
       (lambda (n d) (tag (make-rat n d))))
  'done)
(define (make-rational n d)
  ((get 'make 'rational) n d))



(define (install-complex-package)
  ;; 直交形式パッケージと極形式パッケージからインポートした手続き
  (define (make-from-real-imag x y)
    204
    ((get 'make-from-real-imag 'rectangular) x y))
  (define (make-from-mag-ang r a)
    ((get 'make-from-mag-ang 'polar) r a))
  ;; 内部手続き
  (define (add-complex z1 z2)
    (make-from-real-imag (+ (real-part z1) (real-part z2))
                         (+ (imag-part z1) (imag-part z2))))
  (define (sub-complex z1 z2)
    (make-from-real-imag (- (real-part z1) (real-part z2))
                         (- (imag-part z1) (imag-part z2))))
  (define (mul-complex z1 z2)
    (make-from-mag-ang (* (magnitude z1) (magnitude z2))
                       (+ (angle z1) (angle z2))))
  (define (div-complex z1 z2)
    (make-from-mag-ang (/ (magnitude z1) (magnitude z2))
                       (- (angle z1) (angle z2))))
  ;; システムのほかの部分とのインターフェイス
  (define (tag z) (attach-tag 'complex z))
  (put 'add '(complex complex)
       (lambda (z1 z2) (tag (add-complex z1 z2))))
  (put 'sub '(complex complex)
       (lambda (z1 z2) (tag (sub-complex z1 z2))))
  (put 'mul '(complex complex)
       (lambda (z1 z2) (tag (mul-complex z1 z2))))
  (put 'div '(complex complex)
       (lambda (z1 z2) (tag (div-complex z1 z2))))
  (put 'make-from-real-imag 'complex
       (lambda (x y) (tag (make-from-real-imag x y))))
  (put 'make-from-mag-ang 'complex
       (lambda (r a) (tag (make-from-mag-ang r a))))
  'done)

by tube

練習問題2.81

ちなみに put-coercion, get-coercion は新しいテーブルを持てばいいので

(define coercion-table (make-table ))
(define get-coercion (coercion-table 'lookup-proc ))
(define put-coercion (coercion-table 'insert-proc! ))

a.
型変換メソッドが存在し続ける、つまり apply-generic 内にある cond 条件文の else 節に入らないため、無限ループがおきる。

b.
そのままで動く。

c.

(define (apply-generic op . args)
  (let (( type-tags (map type-tag args )))
    (let ((proc (get op type-tags )))
      (if proc
          (apply proc (map contents args))
          (if (= (length args) 2)
              (let (( type1 (car type-tags ))
                    (type2 (cadr type-tags ))
                    (a1 (car args ))
                    (a2 (cadr args )))
                (if (equal? type1 type2)
                    (error "No method for these types")
                (let (( t1->t2 (get-coercion type1 type2 ))
                      (t2->t1 (get-coercion type2 type1 )))
                  (cond (t1->t2
                         (apply-generic op (t1->t2 a1) a2))
                        (t2->t1
                         (apply-generic op a1 (t2->t1 a2)))
                        (else (error "No method for these types"
                                     (list op type-tags )))))))
              (error "No method for these types"
                     (list op type-tags )))))))