CS 119 Lab 7 – Data Directed Programming
Objectives
1. We wish to create generic operators add, sub, mult, and div which perform addition, subtraction, multiplication, and division for operands of any type. We will use data directed programming to achieve this.
Copy the files Cpkg.scm, Table.scm, and lab7.scm from the public folder. These files contain the get and put and the complex arithmetic functions which we have discussed in class. Initially we will allow our generic operators to operate on either regular numbers or on complex numbers. In order to achieve this, we must attach a manifest type to each kind of number. We will place the appropriate procedures for each type into our table and then our generic operators will dispatch the appropriate procedure from the table.
Evaluate the code in the given files. Use put to enter the appropriate procedures into our table. For example, (put ‘number ‘add add-number) and (put ‘complex ‘add add-complex).
2. Consider the code for operate and the definitions of our generic operators. The operate function simply checks the manifest types of the two arguments. If they are equal then it retrieves the appropriate function from the table. We are skirting the issue of what happens if the operands are of different types.
Test out the generic operators with the following:
(add (make-number 1) (make-number 2))
(add (make-complex (make-rectangular 3 4)) (make-complex (make-polar 1 0)))
(add (make-number 1) (make-complex (make-rectangular 3 4)))
3. Now we want to include rationals in our generic package. Copy the file Rpkg.scm from the public folder. This file contains the rational arithmetic functions.
|
Assignment: |
4. Suppose we want to include another generic operator equ? which compares the equality of its two arguments.
|
Assignment: |
5. Email your files containing the assignments to jzimmerm@goucher.edu.