SICP問題2.95
非整数演算を含んだGCDの動作
(define p1 (make-polynomial 'x '((2 1) (1 -2) (0 1)))) ; p1 p1 ; (polynomial x (2 1) (1 -2) (0 1)) (define p2 (make-polynomial 'x '((2 11) (0 7)))) ; p2 p2 ; (polynomial x (2 11) (0 7)) (define p3 (make-polynomial 'x '((1 13) (0 5)))) ; p3 p3 ; (polynomial x (1 13) (0 5)) (define q1 (mul p1 p2)) ; q1 q1 ; (polynomial x (4 11) (3 -22) (2 18) (1 -14) (0 7)) (define q2 (mul p1 p3)) ; q2 q2 ; (polynomial x (3 13) (2 -21) (1 3) (0 5)) (greatest-common-divisor q1 q2) ; (polynomial x (2 1458/169) (1 -2916/169) (0 1458/169))
gcd-termsをトレースするように修正
(define (install-polynomial-package) (define (make-poly variable termlist) (cons variable termlist)) (define (variable p) (car p)) (define (term-list p) (cdr p)) (define (variable? x) (symbol? x)) (define (same-variable? v1 v2) (and (variable? v1) (variable? v2) (eq? v1 v2))) (define (add-terms L1 L2) (cond ((empty-termlist? L1) L2) ((empty-termlist? L2) L1) (else (let ((t1 (first-term L1)) (t2 (first-term L2))) (cond ((> (order t1) (order t2)) (adjoin-term t1 (add-terms (rest-terms L1) L2))) ((< (order t1) (order t2)) (adjoin-term t2 (add-terms L1 (rest-terms L2)))) (else (adjoin-term (make-term (order t1) (add (coeff t1) (coeff t2))) (add-terms (rest-terms L1) (rest-terms L2))))))))) (define (mul-terms L1 L2) (if (empty-termlist? L1) (the-empty-termlist) (add-terms (mul-term-by-all-terms (first-term L1) L2) (mul-terms (rest-terms L1) L2)))) (define (mul-term-by-all-terms t1 L) (if (empty-termlist? L) (the-empty-termlist) (let ((t2 (first-term L))) (adjoin-term (make-term (add (order t1) (order t2)) (mul (coeff t1) (coeff t2))) (mul-term-by-all-terms t1 (rest-terms L)))))) (define (adjoin-term term termlist) (if (=zero? (coeff term)) termlist (cons term termlist))) (define (the-empty-termlist) '()) (define (first-term termlist) (car termlist)) (define (rest-terms termlist) (cdr termlist)) (define (empty-termlist? termlist) (null? termlist)) (define (make-term order coeff) (list order coeff)) (define (order term) (car term)) (define (coeff term) (cadr term)) (define (div-terms L1 L2) (if (empty-termlist? L1) (list (the-empty-termlist) (the-empty-termlist)) (let ((t1 (first-term L1)) (t2 (first-term L2))) (if (> (order t2) (order t1)) (list (the-empty-termlist) L1) (let ((new-c (div (coeff t1) (coeff t2))) (new-o (- (order t1) (order t2)))) (let ((rest-of-result (div-terms (add-terms L1 (negate-termlist (mul-terms (list (make-term new-o new-c)) L2))) L2) )) (list (add-terms (list (make-term new-o new-c)) (car rest-of-result)) (cadr rest-of-result)) )))))) (define (gcd-terms a b) ; トレースするために以下を追加 (display (list 'parameters a b)) (newline) (if (empty-termlist? b) a (gcd-terms b (remainder-terms a b)))) (define (remainder-terms a b) (cadr (div-terms a b))) (define (add-poly p1 p2) (if (same-variable? (variable p1) (variable p2)) (make-poly (variable p1) (add-terms (term-list p1) (term-list p2))) (error "Polys not in same var -- ADD-POLY" (list p1 p2)))) (define (mul-poly p1 p2) (if (same-variable? (variable p1) (variable p2)) (make-poly (variable p1) (mul-terms (term-list p1) (term-list p2))) (error "Polys not in same var -- MUL-POLY" (list p1 p2)))) (define (div-poly p1 p2) (if (same-variable? (variable p1) (variable p2)) (make-poly (variable p1) (div-terms (term-list p1) (term-list p2))) (error "Polys not in same var -- DIV-POLY" (list p1 p2)))) (define (gcd-poly p1 p2) (if (same-variable? (variable p1) (variable p2)) (make-poly (variable p1) (gcd-terms (term-list p1) (term-list p2))) (error "Polys not in same var -- GCD-POLY" (list p1 p2)))) (define (=zero-poly? L) (define (=zero-term? x) (or (empty-termlist? x) (if (=zero? (coeff (first-term x))) (=zero-term? (rest-terms x)) #f))) (=zero-term? (term-list L))) (define (negate-termlist L) (if (empty-termlist? L) L (let ((f (first-term L)) (r (rest-terms L))) (adjoin-term (make-term (order f) (negate (coeff f))) (negate-termlist r))))) (define (sub-poly x y) (add-poly x (make-poly (variable y) (negate-termlist (term-list y))))) ;; システムの他の部分とのインタフェース (define (tag p) (attach-tag 'polynomial p)) (put 'add '(polynomial polynomial) (lambda (p1 p2) (tag (add-poly p1 p2)))) (put 'mul '(polynomial polynomial) (lambda (p1 p2) (tag (mul-poly p1 p2)))) (put 'make 'polynomial (lambda (var terms) (tag (make-poly var terms)))) (put '=zero? '(polynomial) (lambda (x) (=zero-poly? x))) (put 'negate '(polynomial) (lambda (x) (tag (make-poly (variable x) (negate-termlist (term-list x)))))) (put 'sub '(polynomial polynomial) (lambda (p1 p2) (tag (sub-poly p1 p2)))) (put 'div '(polynomial polynomial) (lambda (p1 p2) (tag (div-poly p1 p2)))) (put 'greatest-common-divisor '(polynomial polynomial) (lambda (x y) (tag (gcd-poly x y)))) 'done) (install-polynomial-package) (define (make-polynomial var terms) ((get 'make 'polynomial) var terms))
テスト
(greatest-common-divisor q1 q2) ; (parameters ((4 11) (3 -22) (2 18) (1 -14) (0 7)) ((3 13) (2 -21) (1 3) (0 5))) ; (parameters ((3 13) (2 -21) (1 3) (0 5)) ((2 1458/169) (1 -2916/169) (0 1458/169))) ; (parameters ((2 1458/169) (1 -2916/169) (0 1458/169)) ()) ; (polynomial x (2 1458/169) (1 -2916/169) (0 1458/169))