(define (the-empty-termlist) '())
(define (make-term order coeff) (list order coeff))
(define (order term) (car term))
(define (coeff term) (cadr term))
(define (adjoin-term-dense term-list)
(define (adjoin-term term)
(let ((len (- (order term) (order (first-term term-list)))))
(define (adjoin-zero-iter term-list times)
(if (= 0 times)
term-list
(adjoin-zero-iter (cons 0 term-list) (- times 1))))
(adjoin-zero-iter term-list len)))
adjoin-term)
(define (first-term-dense term-list)
(let ((order (- (lenth term-list) 1))
(coeff (car term-list)))
(make-term order coeff))))
(define (rest-terms-dense term-list)
(cdr term-list))
(define (empty-termlist-dense? term-list)
(null? term-list))
(define (adjoin-term-sparse term-list)
(define (adjoin-term term)
(if (=zero? term)
term-list
(cons term term-list)))
adjoin-term)
(define (first-term-sparse term-list)
((car term-list)))
(define (rest-terms-sparse term-list)
(cdr term-list))
(define (empty-termlist-sparse? term-list)
(null? term-list))
(put 'adjoin-term-proc 'dense adjoin-term-dense)
(put 'adjoin-term-proc 'sparse adjoin-term-sparse)
(put 'first-term 'dense first-term-dense)
(put 'first-term 'sparse first-term-sparse)
(put 'rest-terms 'dense rest-terms-dense)
(put 'rest-terms 'sparse rest-terms-sparse)
(put 'empty-termlist? 'dense empty-termlist-dense?)
(put 'empty-termlist? 'sparse empty-termlist-sparse?)
(define (adjoin-term term term-list)
(define (adjoin-term-proc term-list)
(apply-generic 'adjoin-term-proc term-list))
((adjoin-term-proc term-list) term))
(define (first-term term-list)
(apply-generic 'first-term term-list))
(define (rest-term term-list)
(apply-generic 'rest-term term-list))
(define (empty-termlist? term-list)
(apply-generic 'empty-termlist? term-list))