In this exercise, we deal with the reimplementation of the deriv
program that we had worked on before to use the the data-derected dispatch.
We are given the following code which does that:-
The first part of the question asks us to describe what is happening in the code above and why the predicates for number?
and variable?
cannot be combined into the data-direction dispatch described in this section.
In the code above, when given an expression, we first use the built-in function number?
to check if the expression is a number and return 0. Then, when given a symbol, we check if it is the same as the variable with we are differentiating with respect to and returns 0 or 1. Finally, we user the operator
function to obtain the operator of the expression which can be '+
, or '*
as we had seen before. We then use the get
function to look up the exact deriv
procedure define for the particular expression and apply them to the expression.
Now, we are also asked why the number and variable branches cannot be merged. The reason for that is because these expressions are the primitive parts which do not have a type tag. So there is no dispatching based on the operator type. They are therefore they are dispatched based on their data type.
In the second part of the exercise, we are told to write the procedures for derivatives of sums and products along with the code to install them in the table to be used by the given code. For that purpose, let us implement a the dispatch table using the built-in hashtable
which supports the put
and get
operations.
With the dispatch table in hand, let us tackle the next problem at hand.
Using the above code, we initialize everything the deriv
program needs. We can install the package by calling install-deriv-package
. Let us see test it.
In the third part of the exercise, we are told to create an additional package such as one for differentiating exponents and install it into the system. Let us write the code which does that.
By simply installing the new functions into the dispatch table, we extend the functionality of the deriv
system. Note that we have retyped some internal functions such as =number?
and make-product
. The constructors need to be implemented globally so that they can be reused. Further, any upgrade to these procedures can be done in one location and the changes can propogate consistently.
In the final part of the exercise, we ae asked how we can change the order of indexing in get
by using the following code instead.
We can utilize the function as is by changing the order in which the index to the hash-table is constructed in the get
function.
By changing the order in the function signature, this can be implemented efficiently. Thus, this layer of abstraction lets us make modifications.
In this exercise, we are told of a system set up by Insatiable Enterprises, Inc., which is a networked system with each component having its own way of storing records. We then look at ways to integrate them.
The first part of the exercise asks us how to implement a get-record
procedure that can retreive an employee’s record from various disparate computer systems. First, we need a way of creating tagged records which specify their storage scheme.
Next, we need to retreive the specific operation that retreives the data and apply it to the file.
With this, we can get record from any installed file system. Each division is in charge of creating the appropriate functions and files with correct tagged types.
In this part of the question, we are tasked with implementing a get-salary
procedure which returns the salary information from a given employee’s record. Similar to how we created the tagged data and respective retreival methods in the previous part, we tackle this problem the same way.
Again, its the responsibility of each division to tag its record and install appropriate functions in the dispatch table to retreive the salient information. If it is to be combined with the employee record retreival from the previous part, we can skip tagging each individual record by reusing the division
information.
In the third part of the exercise, we are tasked with implementing a function called find-employee-record
which takes an employee and a list of files, and returns an employee record. We can reuse the retreival functions defined in the first part. We add one more function which checks if the given employee is found in a file.
In the last part of the exercise, we are asked what needs to be done when Insatiable takes over a new company and needs to incorporate its records into its system.
The answer is quite simple. The new data needs to be tagged with their division names and their versions of get-record
, get-salary
and record-present?
need to be installed in the dispatch table.
In this exercise, we are told to implement the constructor make-from-mag-ang
analogous to the given make-from-real-imag
constructor using the message-passing style.
This exercise deal with ways to evolve a large system and grow it using three strategies - generic operations with explicit dispatch, data-directed style and message-passing style and asks us to compare them.
Based on what we have learnt so far in this section, reducing modification to existing code base and only extension is a wiser decision because of its simplicity. Thus, both data-directed style and message-passing style are recommended for large systems. They are equivalent in power.