(wat-aro)

生きてます

2015-10-31から1日間の記事一覧

SICP 問題 2.81

;; a (define (apply-generic op . args) (let ((type-tags (map type-tag args))) (let ((proc (get op type-tags))) (if proc ;;false (apply proc (map contents args)) (if (= (length args) 2) (let ((type1 (car type-tags)) (type2 (cadr type-tags))…

SICP 問題 2.80

(define (=zero? x) (apply-generic '=zero? x y)) ;; scheme-numberパッケージに追加 (put '=zero? '(scheme-number) (lambda (x) (= x 0))) ;; rationalパッケージに追加 (put '=zero? '(rational) (lambda (x) (= (numer x) 0))) ;; complexパッケージに…

SICP 問題 2.79

;; scheme-numberパッケージに追加 (put 'equ? '(scheme-number scheme-number) (lambda (x y) (tag (= x y)))) ;; rationalパッケージに追加 (put 'equ? '(rational rational) (lambda (x y) (and (= (numer x) (numer y)) (= (denom x) (denom y))))) ;; c…

SICP 問題 2.78

(define (attach-tag type-tag contents) (if (eq? type-tag 'scheme-number) contents (cons type-tag contents))) (define (type-tag datum) (cond ((number? (car datum)) 'scheme-number) ((pair? datum) (car datum)) (else (error "Bad tagged datum -…

SICP 問題 2.77

(put 'real-part '(complex) real-part) (put 'imag-part '(complex) imag-part) (put 'magnitude '(complex) magnitude) (put 'angle '(complex) angle) ;; magnitudeはcomplex型を知らないのでerrorを返す. ;; なので表にcomplex型を追加すれば動く. (ma…