SICP Practice

Practice 2.88

(define (negate-scheme-number x)
  (- x))

(define (negate-real x)
  (- x))

(define (negate-rational x)
  (make-rational (- (numerator x)) (- (denominator x))))

(define (negate-complex x)
  (make-complex-from-real-img (- (real-part x)) (- (imag-part x))))
  
(put 'negate 'scheme-number negate-scheme-number)
(put 'negate 'real negate-real)
(put 'negate 'rational negate-rational)
(put 'negate 'complex negate-complex)

(define (negate x) (apply-generic 'negate x))

(define (negate-poly x)
  (make-polynomial (variable x) (negate-term-list (term-list x))))

(define (negate-term-list termlist)
  (if (empty-termlist? termlist)
      (the-empty-termlist)
      (ajoint-term (make-term (order (first-term termlist)) (negate (coeff (first-term termlist))))
                   (negate-term-list (rest-terms termlist)))))

(put 'negate 'polynomial negate-poly)

(put 'sub '(polynomial polynomial)
  (lambda (x y)
    (add x (negate y))))