Chapter 2, Building Abstractions with Data

Section - 2.5 - Systems with Generic Operations

Exercise 2.89


I got something interesting to learn here. The =zero? procedure I implemented for polynomial in solution 2.87 was not working correctly!. Because I was using internal representation of the term-list in =zero? implementation(iterating it using car and cadr - inside list-and). The lesson learned is the code/procedures which are external to term-list api shoud not use the internal representation. Well, it seems obvious to not do such bad things :)

Here are the required changes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(define (adjoin-term term term-list)
      (define (iter count terms)
          (if (= count 0)
              terms
              (iter (- count 1) (cons 0 terms))
          )
       )
       (let ((cof (coeff term))
             (count (- (order term) (length term-list)))
            )
            (cond
                ((=zero? cof) term-list)
                ((< count 0) (error "Can not add term - order of passed term is already present in the list"))
                (else (cons cof (iter count term-list)))
            )
       )
)
(define (first-term term-list) (make-term (- (length term-list) 1) (car term-list)))
;all other procedures of term-list will remain same

And here goes the new =zero? implementation:

1
2
3
4
5
6
7
8
9
(define (=is-zero? poly)
       (define (iter terms)
         (if (empty-termlist? terms)
             #t
             (and (=zero? (coeff (first-term terms))) (iter (rest-terms terms)))
         )
       )
       (iter (term-list poly))
)

Test:

1
2
3
4
5
6
7
> (display (add (make-polynomial 'x (list 2 3)) (make-polynomial 'x (list 4 5))))

(polynomial x (int . 6) (int . 8))
> (display (add (make-polynomial 'x (list 2 3)) (make-polynomial 'x (list 4 5 6))))

(polynomial x 4 (int . 7) (int . 9))
>