;Term format: (deg coef) (define the-zero-poly '((0 0))) (define zero-poly? (lambda (poly) (equal? poly the-zero-poly) ) ) (define degree (lambda (poly) (car (car poly)) ) ) (define leading-coef (lambda (poly) (car (cdr (car poly))) ) ) (define rest-of-poly (lambda (poly) (if (null? (cdr poly)) the-zero-poly ;#t (cdr poly) ;#f ) ) ) ;If n is a nonnegative integer, ; a is a real number, ; p is the-zero-poly OR ; a polynomial of degree less than n, ;then ; (poly-cons n a p) is the polynomial ; obtained by adding the leading term ; ax^n to the polynomial p (define poly-cons (lambda (deg coef poly) (let ((deg-p (degree poly))) (cond ((and (zero? deg) (equal? poly the-zero-poly)) (list (list 0 coef)) ) ((>= deg-p deg) '("Error: Degree of poly too high") ) ((zero? coef) poly) (else (cons (list deg coef) poly) ) ) ) ) ) ;============== Unit Tests ====================== (load "test.scm") (define msg "poly-basic") (test msg (zero-poly? '((0 0))) #t) (test msg (zero-poly? '((1 4) (0 5))) #f) (test msg (leading-coef'((1 4) (0 5))) '4) (test msg (degree'((1 4) (0 5))) '1) (test msg (rest-of-poly '((2 3) (1 4) (0 5))) '((1 4) (0 5))) (test msg (poly-cons 3 1 '((2 3) (1 4) (0 5))) '((3 1) (2 3) (1 4) (0 5))) (test msg (poly-cons 8 1 '((2 3) (1 4) (0 5))) '((8 1) (2 3) (1 4) (0 5))) (test msg (poly-cons 1 3 '((2 3) (1 4) (0 5))) '("Error: Degree of poly too high"))