Exercise 2.87

Read Exercise 2.87 ~ Solution


; in the poly package
(define (terms-zero? terms)
  (if (empty-termlist? terms)
      true
      (and (=zero? (coeff (first-term terms)))
           (terms-zero? (rest-terms terms)))))
(define (poly-zero? p) (terms-zero? (term-list p)))
...
...
(put '=zero?     '(polynomial)            (lambda (p1)          (poly-zero? p1)))

A few basic tests.

(define zero-terms 
  (list (make-term 4 0) 
        (make-term 2 0)
        (make-term 0 0)))
(define +ve-terms 
  (list (make-term 100 1)
        (make-term   2 2)
        (make-term   0 1)))
(define -ve-terms 
  (list (make-term 100 -1)
        (make-term   2 -2)
        (make-term   0 -1)))
(define term-101 (make-term 101 3))

(define pt0  (make-polynomial 'x zero-terms))
(define pt1  (make-polynomial 'x +ve-terms))
(define pt2  (make-polynomial 'y +ve-terms))
(define pt3  (make-polynomial 'x (cons term-101 +ve-terms)))
(define -pt1 (make-polynomial 'x -ve-terms))
(define -pt2 (make-polynomial 'y -ve-terms))

(define poly-py1 (make-polynomial 'y (list (make-term 3 pt1) (make-term 1 pt2) (make-term 0 pt3))))
(define poly-py2 (make-polynomial 'y (list (make-term 3 pt3) (make-term 2 pt1))))

(display (map =zero? (list pt0 pt1 pt2 pt3 -pt1 -pt2 poly-py1 poly-py2)))
(#t #f #f #f #f #f #f #f)

(show (add pt1 pt0))
(polynomial x (100 1) (2 2) (0 1))

(show (add pt1 pt3))
(polynomial x (101 3) (100 2) (2 4) (0 2))

(show (add poly-py1 poly-py2))
(polynomial y (3 (polynomial x (101 3) (100 2) (2 4) (0 2))) (2 (polynomial x (100 1) (2 2) (0 1))) (1 
(polynomial y (100 1) (2 2) (0 1))) (0 (polynomial x (101 3) (100 1) (2 2) (0 1))))

(show (mul pt1 pt3))
(polynomial x (201 3) (200 1) (103 6) (102 4) (101 3) (100 2) (4 4) (2 4) (0 1))

(show (mul pt0 pt3))
(polynomial x)

Leave a comment