# set-1

## Exercise 2.91

A univariate polynomial can be divided by another one to produce a polynomial quotient and a polynomial remainder. For example,

Division can be performed via long division. That is, divide the highest-order term of the dividend by the highest-order term of the divisor. The result is the first term of the quotient. Next, multiply the result by the divisor, subtract that from the dividend, and produce the rest of the answer by recursively dividing the difference by the divisor. Stop when the order of the divisor exceeds the order of the dividend and declare the dividend to be the remainder. Also, if the dividend ever becomes zero, return zero as both quotient and remainder.

We can design a `div-poly`

procedure on the model of `add-poly`

and `mul-poly`

. The procedure checks to see if the two polys have the same variable. If so, `div-poly`

strips off the variable and passes the problem to `div-terms`

, which performs the division operation on term lists. `Div-poly`

finally reattaches the variable to the result supplied by `div-terms`

. It is convenient to design `div-terms`

to compute both the quotient and the remainder
of a division. `Div-terms`

can take two term lists as arguments and return a list of the quotient term list and the remainder term list.

Complete the following definition of `div-terms`

by filling in the missing expressions. Use this to implement `div-poly`

, which takes two polys as arguments and returns a list of the quotient and
remainder polys.

```
(define (div-terms L1 L2)
(if (empty-termlist? L1)
(list (the-empty-termlist) (the-empty-termlist))
(let ((t1 (first-term L1))
(t2 (first-term L2)))
(if (> (order t2) (order t1))
(list (the-empty-termlist) L1)
(let ((new-c (div (coeff t1) (coeff t2)))
(new-o (- (order t1) (order t2))))
(let ((rest-of-result
<
````compute rest of result recursively`>
))
<`form complete result`>
))))))

## Exercise 2.90

Suppose we want to have a polynomial system that is efficient for both sparse and dense polynomials. One way to do this is to allow both kinds of term-list representations in our system. The situation is analogous to the complex-number example of section 2.4, where we allowed both rectangular and polar representations. To do this we must distinguish different types of term lists and make the operations on term lists generic. Redesign the polynomial system to implement this generalization. This is a major effort, not a local change.

## Exercise 2.89

Define procedures that implement the term-list representation described above as appropriate for dense polynomials.

## Exercise 2.88

Extend the polynomial system to include subtraction of polynomials. (Hint: You may find it helpful to define a generic negation operation.)

## Exercise 2.87

Install `=zero?`

for polynomials in the generic arithmetic package. This will allow `adjoin-term`

to work for polynomials with coefficients that are themselves polynomials.

## Exercise 2.74

Insatiable Enterprises, Inc., is a highly decentralized conglomerate company consisting of a large number of independent divisions located all over the world. The company’s computer facilities have just been interconnected by means of a clever network-interfacing scheme that makes the entire network appear to any user to be a single computer. Insatiable’s president, in her first attempt to exploit the ability of the network to extract administrative information from division files, is dismayed to discover that, although all the division files have been implemented as data structures in Scheme, the particular data structure used varies from division to division. A meeting of division managers is hastily called to search for a strategy to integrate the files that will satisfy headquarters’ needs while preserving the existing autonomy of the divisions.

Show how such a strategy can be implemented with data-directed programming. As an example, suppose that each division’s personnel records consist of a single file, which contains a set of records keyed on employees’ names. The structure of the set varies from
division to division. Furthermore, each employee’s record is itself a set (structured differently from division to division) that contains information keyed under identifiers such as `address`

and `salary`

. In particular:

Implement for headquarters a

`get-record`

procedure that retrieves a specified employee’s record from a specified personnel file. The procedure should be applicable to any division’s file. Explain how the individual divisions’ files should be structured. In particular, what type information must be supplied?Implement for headquarters a

`get-salary`

procedure that returns the salary information from a given employee’s record from any division’s personnel file. How should the record be structured in order to make this operation work?Implement for headquarters a

`find-employee-record`

procedure. This should search all the divisions’ files for the record of a given employee and return the record. Assume that this procedure takes as arguments an employee’s name and a list of all the divisions’ files.When Insatiable takes over a new company, what changes must be made in order to incorporate the new personnel information into the central system?

## Exercise 2.73

Section 2.3.2 described a program that performs symbolic differentiation:

```
(define (deriv exp var)
(cond ((number? exp) 0)
((variable? exp) (if (same-variable? exp var) 1 0))
((sum? exp)
(make-sum (deriv (addend exp) var)
(deriv (augend exp) var)))
((product? exp)
(make-sum
(make-product (multiplier exp)
(deriv (multiplicand exp) var))
(make-product (deriv (multiplier exp) var)
(multiplicand exp))))
<
````more rules can be added here`>
(else (error "unknown expression type -- DERIV" exp))))

We can regard this program as performing a dispatch on the type of the expression to be differentiated. In this situation the “type tag” of the datum is the algebraic operator symbol (such as `+`

) and the operation being performed is `deriv`

. We can transform this program into data-directed style by rewriting the basic derivative procedure as

```
(define (deriv exp var)
(cond ((number? exp) 0)
((variable? exp) (if (same-variable? exp var) 1 0))
(else ((get 'deriv (operator exp)) (operands exp)
var))))
(define (operator exp) (car exp))
(define (operands exp) (cdr exp))
```

Explain what was done above. Why can’t we assimilate the predicates

`number?`

and`same-variable?`

into the data-directed dispatch?Write the procedures for derivatives of sums and products, and the auxiliary code required to install them in the table used by the program above.

Choose any additional differentiation rule that you like, such as the one for exponents (exercise 2.56), and install it in this data-directed system.

In this simple algebraic manipulator the type of an expression is the algebraic operator that binds it together. Suppose, however, we indexed the procedures in the opposite way, so that the dispatch line in

`deriv`

looked like`((get (operator exp) 'deriv) (operands exp) var)`

What corresponding changes to the derivative system are required?

## Exercise 2.62

Give a Θ(`n`) implementation of `union-set`

for sets represented as ordered lists.

## Exercise 2.61

Give an implementation of `adjoin-set`

using the ordered
representation. By analogy with `element-of-set?`

show how to
take advantage of the ordering to produce a procedure that requires on the average about half as many steps as with the unordered representation.

## Exercise 2.44

Define the procedure `up-split`

used by `corner-split`

.
It is similar to `right-split`

, except that it switches the
roles of `below`

and `beside`

.