计算机程序的构造和解释 练习题2.89

主要修改多项式这里的过程

(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 L1) (order L2))
                 (adjoin-term
                  t1 (add-terms (rest-terms L1) L2)))
                 ((< (order L1) (order L2))
                  (adjoin-term
                   t2 (add-terms L1 (rest-terms L2))))
                 (else
                  (adjoin-term
                   (add (coeff L1) (coeff L2))
                   (add-terms (rest-terms L1)
                              (rest-terms L2)))))))))
(define (sub-terms L1 L2)
  (cond ((empty-termlist? L1) L2)
        ((empty-termlist? L2) L1)
        (else
         (let ((t1 (first-term L1)) (t2 (first-term L2)))
           (cond ((> (order L1) (order L2))
                 (adjoin-term
                  t1 (sub-terms (rest-terms L1) L2)))
                 ((< (order L1) (order L2))
                  (adjoin-term
                   t2 (sub-terms L1 (rest-terms L2))))
                 (else
                  (adjoin-term
                   (sub (coeff L1) (coeff L2))
                   (sub-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 L1 L2)
                 (mul-terms (rest-terms L1) L2))))
  
(define (mul-term-by-all-terms L1 L2)
  (if (empty-termlist? L2)
      (if (= (length L1) 1) (the-empty-termlist) (make-term-list (- (order L1) 1) 0))
      (let ((t1 (first-term L1))
            (t2 (first-term L2)))
        (adjoin-term
         (mul t1 t2)
         (mul-term-by-all-terms L1 (rest-terms L2))))))
(define (adjoin-term term term-list)
      (cons term term-list))
(define (the-empty-termlist) '())
(define (first-term term-list) (car term-list))
(define (rest-terms term-list) (cdr term-list))
(define (empty-termlist? term-list) (null? term-list))
(define (make-term-list order coeff) 
  (define (iter n term-list)
    (if (= n order) (cons coeff term-list)
          (iter (+ n 1) (cons 0 term-list))))
  (iter 0 '()))
(define (order term-list) (- (length term-list) 1))
(define (coeff term-list) (car term-list))

完整过程

#lang racket
;put get实现
(define *op-table* (make-hash))

(define (put op type proc)
  (hash-set! *op-table* (list op type) proc))

(define (get op type)
  (hash-ref *op-table* (list op type) #f))

(define (attach-tag type-tag contents)
  (cond ((eq? type-tag 'scheme-number) contents)
        (else (cons type-tag contents))))

(define (type-tag datum)
  (cond ((number? datum) 'scheme-number)
        ((pair? datum) (car datum))
        (else (error "Bad tagged datum -- TYPE-TAG" datum))))

(define (contents datum)
   (cond ((number? datum) datum)
         ((pair? datum) (cdr datum))
         (else (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"
                                (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 (=zero? x) (apply-generic '=zero? x))



(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 '=zero? '(scheme-number)
       (lambda (x) (= x 0)))
  (put 'make 'scheme-number
       (lambda (x) (tag x)))
  'done)
(define (make-scheme-number n)
  ((get 'make 'scheme-number) n))

(define (install-polynomial-package)
  (define (make-poly variable term-list)
    (cons variable term-list))
  (define (variable p) (car p))
  (define (term-list p) (cdr p))
  (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 "Poly not in same var -- ADD-POLY"
               (list p1 p2))))
  (define (sub-poly p1 p2)
    (if (same-variable? (variable p1) (variable p2))
        (make-poly (variable p1)
                   (sub-terms (term-list p1)
                              (term-list p2)))
        (error "Poly not in same var -- SUB-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 (=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-terms term-list))
              #f)))
    (coeff-all-zero? (term-list poly)))
  (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 'sub '(polynomial polynomial)
       (lambda (p1 p2) (tag (sub-poly p1 p2))))
  (put '=zero? '(polynomial) =zero-poly?)
  (put 'make 'polynomial
       (lambda (var terms) (tag (make-poly var terms))))
  'done)
(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 L1) (order L2))
                 (adjoin-term
                  t1 (add-terms (rest-terms L1) L2)))
                 ((< (order L1) (order L2))
                  (adjoin-term
                   t2 (add-terms L1 (rest-terms L2))))
                 (else
                  (adjoin-term
                   (add (coeff L1) (coeff L2))
                   (add-terms (rest-terms L1)
                              (rest-terms L2)))))))))
(define (sub-terms L1 L2)
  (cond ((empty-termlist? L1) L2)
        ((empty-termlist? L2) L1)
        (else
         (let ((t1 (first-term L1)) (t2 (first-term L2)))
           (cond ((> (order L1) (order L2))
                 (adjoin-term
                  t1 (sub-terms (rest-terms L1) L2)))
                 ((< (order L1) (order L2))
                  (adjoin-term
                   t2 (sub-terms L1 (rest-terms L2))))
                 (else
                  (adjoin-term
                   (sub (coeff L1) (coeff L2))
                   (sub-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 L1 L2)
                 (mul-terms (rest-terms L1) L2))))
  
(define (mul-term-by-all-terms L1 L2)
  (if (empty-termlist? L2)
      (if (= (length L1) 1) (the-empty-termlist) (make-term-list (- (order L1) 1) 0))
      (let ((t1 (first-term L1))
            (t2 (first-term L2)))
        (adjoin-term
         (mul t1 t2)
         (mul-term-by-all-terms L1 (rest-terms L2))))))
(define (adjoin-term term term-list)
      (cons term term-list))
(define (the-empty-termlist) '())
(define (first-term term-list) (car term-list))
(define (rest-terms term-list) (cdr term-list))
(define (empty-termlist? term-list) (null? term-list))
(define (make-term-list order coeff) 
  (define (iter n term-list)
    (if (= n order) (cons coeff term-list)
          (iter (+ n 1) (cons 0 term-list))))
  (iter 0 '()))
(define (order term-list) (- (length term-list) 1))
(define (coeff term-list) (car term-list))
(define (make-polynomial var terms)
  ((get 'make 'polynomial) var terms))

(define (same-variable? v1 v2)
  (define (variable? x) (symbol? x))
  (and (variable? v1) (variable? v2) (eq? v1 v2)))
(install-scheme-number-package)
(install-polynomial-package)
(define polynumial-A (make-polynomial 'x '(5 2 4 5 6 7)))
(define polynumial-B (make-polynomial 'x '(5 2 4 5 6 7)))
(define polynumial-C (make-polynomial 'x '(2 1)))
(add polynumial-A polynumial-B)
(sub polynumial-A polynumial-B)
(mul polynumial-B polynumial-C)
                                 

运行结果

'done
'done
'(polynomial x 10 4 8 10 12 14)
'(polynomial x 0 0 0 0 0 0)
'(polynomial x 10 9 10 14 17 20 7)
weixin063传染病防控宣传微信小程序系统的设计与实现+springboot后端毕业源码案例设计 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值