SICP 2.5 exercises cont. 2

2.87

  (define (zero-terms? term-list)
    (cond ((empty-termlist? term-list) #t)
          ((not (= 0 (coeff (first-term term-list)))) #f)
          (else (zero-terms? (rest-terms term-list)))))
  (put '=zero? '(polynomial) (lambda (p) (zero-terms? (term-list p))))

Decently straightforward.

2.88
Define subtraction in terms of generic add and generic negate. Then implement the negation for polynomials.

(define (sub x y)
  (add x (negate y)))

(define (negate-terms term-list)
    (if (empty-termlist? term-list)
        (the-empty-termlist)
        (cons (make-term (order (first-term term-list))
                         (- (coeff (first-term term-list))))
              (negate-terms (rest-terms term-list)))))
(define (negate-poly p)
    (make-polynomial (variable p) (negate-terms (term-list p))))

It’s Chinese New Year so I had some stuff to do, like eating lots.

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s