Lab 6

Goals

1. Learn how to implement a new data type.

2. Get experience working with symbols and symbolic expressions.

3. Define a simple symbolic integration function.

Experiments

1. The new data type will be polymomials in which the only variable is the symbol x and all the constants are numbers. The only math functions we will allow are addition (+), multiplication (*) and power (**). These functions will all be assumed to have exactly two arguments. For our representation, we will use infix notation for all of these operators. Thus, the representation for 2 + x will be (2 + x), the representation for 2x will be (2 * x), the representation for x2 + 3x will be ((x ** 2) + (3 * x)), etc.

The formal definition of the polynomial data type you are to implement is as follows:

  1. A number is a polynomial.
  2. The symbol x is a polynomial.
  3. If poly1 and poly2 are polynomials, then the following are polynomials: (poly1 + poly2), (poly1 * poly2), and (poly1 ** poly2) are polynomials.
  4. In the case (poly1 * poly2), poly1 will be expected to be a number. (You do not have to test for this, just assume that it is true.)
  5. In the case (poly1 ** poly2), poly1 will be expected to be the symbol x and poly2 will be expected to be a positive number. (You do not have to test whether this is true; just assume that it is true.)

To implement the polynomial data type, implement the following constructors, selectors and predicates:

(You don't need a (power-arg1 x) selector because it is assumed to always return x.

The only reduction rule you need to include is one where the coefficient of a term changes; multiply the appropriate numbers together instead of leaving the two numbers explicitly in the output polynomial. (No nested multiplication expressions.) See example below for an implicit illustration.

2. Now define the service operator integral which symbolically integrates a polynomial. It will not bother to add on an arbitrary constant as is usually done in math classes. This function should be straight-forward to write if you implement and use the constructors, selectors and predicates listed above, and use a cond special form to handle all the cases that must be treated.

Here is an example of how these functions should work:

> (define poly (make-sum (make-power 'x 2) (make-product 3 'x)))
> poly
((x ** 2) + (3 * x))
> (integral poly)
((0.3333333333333333 * (x ** 3)) + (1.5 * (x ** 2)))
(Depending on how you define integral you may get 1/3 instead of 0.3333333333333333. That will be acceptable.)

3. Submit the definitions of your twelve functions to the Automated Tester and click on the Confirm butten when you get a PASS.