Chapter 2, Building Abstractions with Data
Section - 2.5 - Systems with Generic Operations
Exercise 2.87
We just need to add the following in polynomial package:
1
2
3
4
5
6
7
8
9
(define (=is-zero? poly)
(let ((terms (term-list poly)))
(if (empty-termlist? terms)
#t
(list-and (lambda (term) (=zero? (coeff term))) terms)
)
)
)
(put '=zero? '(polynomial) =is-zero?)
I used ‘list-and’ here. There have been many times, I felt its need in earlier exercises too. The list-and
returns true checks if all the elements passed to it returns true on applying proc. Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(define (list-and proc list)
(define (iter list)
(if (null? list)
#t
(let ((rs (proc (car list))))
(if (boolean? rs)
(if rs (iter (cdr list)) #f)
(error "Proc should return boolean")
)
)
)
)
(cond ((null? list) (error "Operation not defined for 0 items"))
((not (pair? list)) (error "Operation works only for lists"))
(else (iter list))
)
)