sicp-ex-2.89



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


meteorgan

 ;; 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)) 
              terms)) 
             ((= exponent times) 
              (cons (coeff term) terms)) 
             (else (iter-adjoin (+ times 1)  
                                (cons 0 terms)))) 
         (iter-adjoin len term-list))) 

hattivat

 ;; 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)))))