算术包实例:符号代数练习题
练习2.87 以下将对多项式进行算术包构建:
点击查看代码
;;通用操作
(define (add x y) (apply-generic 'add x y))
(define (mul x y) (apply-generic 'mul x y))
(define (=zero? x) (apply-generic '=zero? x))
;;多项式运算中涉及的算术操作包
;;数字部分
(define (install-scheme-number-package)
(define (tag-args op)
(lambda (x y) (attach-tag 'scheme-number (op x y))))
(put 'add '(scheme-number scheme-number) (tag-args +))
(put 'mul '(scheme-number scheme-number) (tag-args *))
(put '=zero? '(scheme-number)
(lambda (x) (= x 0))))
;;符号代数部分
(define (install-polynomial-package)
(define (number->poly variable n)
(make-poly variable (adjoin-term (make-term 0 n) (the-empty-termlist))))
(define (tag x) (attach-tag 'polynomial' x))
(define (put-op name op)
(put 'name '(polynomial polynomial)
(lambda (x y) (tag (op x y))))
(put 'name '(polynomial scheme-number)
(lambda (x y) (tag (op x (number->poly (variable x) y)))))
(put 'name '(scheme-number polynomial)
(lambda (x y) (tag (op (number->poly (variable y) x)) y))))
(put-op 'add add-poly)
(put-op 'mul mul-poly)
(put '=zero? 'polynomial =zero-poly?)
'done)
;;对符号代数中需要的函数进行说明
(define (polynomial? p)
(and (pair? p) (eq? (car p) 'polynomial)))
(define (make-poly variable term-list)
(cons 'polynomial (cons variable term-list)))
(define (variable? p) (symbol? p))
(define (same-variable? p1 p2)
(and (variable? p1) (variable? p2)(eq? p1 p2)))
(define (variable p)(cadr p))
(define (term-list p)(cddr p))
(define (add-poly p1 p2)
(if (same-variable? (variable p1) (variable p2))
(make-poly (variable p1)
(add-terme (term-list p1)
(term-list p2)))
(error "Polys not in same var -- ADD" (list p1 p2))))
(define (mul-poly p1 p2)
(if (same-variable? (variable p1) (variable p2))
(make-poly (variable p1)
(mul-terme (term-list p1)
(term-list p2)))
(error "Polys not in same var -- MUL" (list p1 p2))))
(define (add-terms L1 L2)
(cond ((empty-termlist? L1) L2)
((empty-termlist? L2) L1)
(else
(let ((t1 (first-term L1))
(t2 (firet-term L2)))
(cond ((> (order t1) (order t2))
(adjoin-term t1 (add-terms (rest-terms L1) L2)))
((> (order t2) (order t1))
(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-terms-by-all-terms (first-term L1) L2)
(mul-terms (rest-terms L1) L2))))
(define (mul-terms-by-all-terms t1 L)
(if (empty-termlist? L)
(the-empty-termlist)
(let ((t2 (first-term L)))
(adjoin-term
(make-term (+ (order t1) (order t2))
(mul (coeff t1) (coeff t2)))
(mul-terms-by-all-terms t1 (rest-terms L))))))
(define (adjoin-term term term-list)
(if (=zero? (coeff term))
term-list
(cons term term-list)))
;;零的判断
(define (=zero-poly? poly)
(define (coeff-all-zero? term-list)
(if (empty-termlist? term-list) #t
(if (=zero? (coeff (first-term term-list)))
(coeff-all-zero? (rest-of-terms term-list))
#f)))
(coeff-all-zero? (term-list poly)))
(define (first-term term-list) (car term-list))
(define (rest-of-terms term-list) (cdr term-list))
(define (make-term oredr coeff)(cond oredr coeff))
(define (order term)(car term))
(define (coeff term)(cadr term))
(define (empty-termlist? term-list)(null? term-list))
练习2.88 扩充算术包,加上多项式的减法。
点击查看代码
;;整数包中添加:
(put 'neg '(scheme-number) (lambda (x) (tag (- x))))
;;多项式中添加:
(put-op 'sub sub-poly)
(put 'neg '(polynomial) (lambda (x) (tag (neg-poly x))))
;;定义减法:
(define (sub-poly p1 p2)
(add p1 (neg p2)))
;;通过一个通用的取负操作实现减法
(define (neg-poly p)
(make-poly (variable p)
(neg-term (term-list p))))
(define (neg-term L)
(if (empty-termlist? L)
(the-empty-termlist)
(let ((t (first-term L)))
(adjoin-term (make-term (order t) (neg (coeff t)))
(neg-term (rest-of-terms L))))))

浙公网安备 33010602011771号