sicp-ex-2.87



<< Previous exercise (2.86) | Index | Next exercise (2.88) >>


meterogan

  
  
  
 ;; add into polynomial package 
 (define (zero-poly? poly) 
   (define (zero-terms? termlist) 
     (or (empty-termlist? termlist) 
         (and (=zero? (coeff (first-term termlist))) 
              (zero-terms? (rest-terms termlist))))) 
   (zero-terms? (term-list poly))) 

Don't need zero-terms? because the polynomial representation in the book cannot have a term-list with zero terms (see definition for adjoin-term)

 (define (zero-poly? p) 
    (empty-termlist? (term-list p))) 
  


Siki

The coefficient of a term can be either a number or a polynomial (Actually a number is a special polynomial with zero order). If it's number, we use the predicate (= x 0); If it's represented as a polynomial, it cannot be zero. The polynomial representation we adopt does not contain zero terms.

  
 ;;to be installed in polynomial package 
 (define (=zero? x) 
   (define (poly? x) 
     (pair? x)) 
   (cond ((number? x) (= x 0)) 
         ((poly? x) false) 
         (else (error "Unknown type")))) 
  
 (put '=zero? 'polynomial =zero?) 
  

It's not necessary to do these tests, =zero? is a generic procedure which works on any type of argument, it will send the argument to the right place to do the comparison. Also, this procedure is not correct because it (falsely) assumes that there is no such thing as a polynomial with value zero. In fact, it is possible to have an arbitrarily long polynomial with all zero coefficients. It will be simplified to zero, sure, but we don't know whether the input is simplified or not. I think the following procedure works (can't test it):

 (define (=zero? poly) 
   (if (empty-termlist? poly) 
       (the-empty-termlist) 
       (if (=zero? (coeff (first-term poly))) 
           (=zero? (rest-terms poly)) 
           #f))) 

If it encounters any non-zero coefficient then the polynomial is obviously not equal to zero.