; - Program 5.16, pg. 157 -

(define digits->poly
  (lambda (digit-list)
    (if (null? digit-list)
        (error "digits->poly: Not defined for" digit-list)
        (letrec
          ((make-poly
             (lambda (deg ls)
               (if (null? ls)
                   the-zero-poly
                   (poly-cons deg (car ls)
                     (make-poly (sub1 deg) (cdr ls)))))))
          (make-poly (sub1 (length digit-list)) digit-list)))))

; - End Program -

; - Program 5.17, pg. 157 -

(define binary->decimal
  (lambda (digit-list)
    (poly-value (digits->poly digit-list) 2)))  

; - End Program -

; - Program 5.18, pg. 158 -

(define poly->digits
  (lambda (poly)
    (letrec
      ((convert
         (lambda (p deg)
           (cond
             ((zero? deg) (list (leading-coef p)))
             ((= (degree p) deg)
              (cons (leading-coef p)
                    (convert (rest-of-poly p) (sub1 deg))))
             (else
              (cons 0 (convert p (sub1 deg))))))))
      (convert poly (degree poly)))))

; - End Program -

; - Program 5.20, pg. 160 -

(define decimal->binary
  (lambda (num)
    (letrec
      ((dec->bin
         (lambda (n deg)
           (if (zero? n)
               the-zero-poly
               (p+ (make-term deg (remainder n 2))
                   (dec->bin (quotient n 2) (add1 deg)))))))
      (poly->digits (dec->bin num 0)))))

; - End Program -

