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

SICPゼミ第36回

練習問題2.78からやり直し

練習問題2.78

(define (attach-tag type-tag contents)
  (if (eq? type-tag 'scheme-number)
      contents
      (cons type-tag contents )))
(define (type-tag datum)
  (if (pair? datum)
      (car datum)
      (if (number? datum)
          'scheme-number
          (error "Bad tagged datum: TYPE-TAG" datum ))))
(define (contents datum)
  (if (pair? datum)
      (cdr datum)
      (if (number? datum)
          datum
          (error "Bad tagged datum: CONTENTS" datum ))))

by pine

練習問題2.79

(define (install-equ?)
  (put 'equ? 'scheme-number (lambda (x y) (= x y)))
  (put 'equ? 'rational (lambda (x y) (and (= (car x) (car y))
                                               (= (cdr x) (cdr y)))))
  (put 'equ? 'complex (lambda (x y) (and (= (real-part x) (real-part y))
                                         (= (imag-part x) (imag-part y))))))

練習問題2.80

(define (install-=zero?)
  (put '=zero? 'rational (lambda (x) (= (car x) 0)))
  (put '=zero? 'scheme-number (lambda (x) (= x 0)))
  (put '=zero? 'complex (lambda (x) (and (= (real-part x) 0) (= (imag-part x) 0)))))

by dolicas

練習問題2.87

(put '=zero? 'polynomial (lambda (x) (null? (cdr x))))