; adapted from Program 5.15, pg. 153 -

(define poly-tag 'poly)

(define the-zero-poly (list poly-tag '(0 0)))

(define degree
  (lambda (poly)
    (caar (cdr poly))))
    
(define leading-coef
  (lambda (poly)
    (cadar (cdr poly))))

(define rest-of-poly 
  (lambda (poly)
    (if (null? (cdr (cdr poly)))
        the-zero-poly
        (cons poly-tag (cdr (cdr poly))))))

(define poly-cons
  (lambda (deg coef poly)
    (let ((deg-p (degree poly)))
      (cond 
        ((and (zero? deg) (equal? poly the-zero-poly))
         (list poly-tag (list 0 coef)))
        ((< deg-p deg)
         (if (zero? coef) 
             poly
             (cons poly-tag (cons (list deg coef) (cdr poly)))))
        (else (error "poly-cons: degree too high in" poly))))))

; - End Program -
