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


 ;; all we should change are procedure first-term and adjoin-term 
 (define (first-term term-list) 
   (list (car term-list) (- (length term-list) 1))) 
 (define (adjoin-term term term-list) 
   (let ((exponent (order term)) 
         (len (length term-list))) 
     (define (iter-adjoin times terms) 
       (cond ((=zero? (coeff term)) 
             ((= exponent times) 
              (cons (coeff term) terms)) 
             (else (iter-adjoin (+ times 1)  
                                (cons 0 terms)))) 
         (iter-adjoin len term-list))) 


 ;; an even shorter solution to achieve the same result 
 (define (first-term term-list) 
     (make-term (- (len term-list) 1) (car term-list))) 
 (define (adjoin-term term term-list) 
   (cond ((=zero? term) term-list) 
         ((=equ? (order term) (length term-list)) (cons (coeff term) term-list)) 
         (else (adjoin-term term (cons 0 term-list))))) 


We don't need to change much. Also, since adjoin-term is properly used in add-term and mul-term, I don't think we have to change it drastically like some people did.

 ;; List the term, and find the order 
 (define (first-term term-list) 
     (car term-list) ; first term 
     (- 1 (length term-list)))) ; order is length-1 (we index polynomials from 0) 
 ;; Slightly modified to cons the coeff instead of the actual term 
 (define (adjoin-term term term-list) 
   (let ((coeff-of-term (coeff term))) 
     (if (=zero? coeff-of-term) 
       (cons coeff-of-term term-list)))) 


It is probably me, but none of the solution above worked for me.

meteorgan's solution has bracketing errors and even after fixing them there seems to be logical errors.

hattivat uses =zero? directly on term which does not work because add-terms calls adjoin-term on regular terms, i.e. one that have coeff and order.

Finally, Marisa's solution runs, but creates false results for my tests.

The following code worked for me when I added it to the poly-package from the book. Note that not considering zero terms would yield invalid results for multiplication.

   (define (first-term term-list) 
     (make-term (- (length term-list) 1) 
                (car term-list))) 
   (define (adjoin-term term term-list) 
     (let ((coeff-term (coeff term)) 
           (order-term (order term)) 
           (length-terms (length term-list))) 
         ((= order-term length-terms) (cons coeff-term term-list)) 
         ((< order-term length-terms) (error "Cannot adjoin lower-order term to terms")) 
         (else (cons coeff-term (adjoin-term (make-term (- order-term 1) 0) term-list))))))