SICP Exercise 3.25 make-table generalized

Exercise 3.25.  Generalizing one- and two-dimensional tables, show how to implement a table in which values are stored under an arbitrary number of keys and different values may be stored under different numbers of keys. The lookup and insert! procedures should take as input a list of keys used to access the table.

SOLUTION

The code is here.

The table structure can be seen here.


Supported table structure is something like:

TABLE
--------
K1: val1
K2: val2
K3: val3
K31: val31
K32: (no value)
K321: val321
K322: val322
K33: val33
K4: val4

So the key combinations to retrieve data would be:

K1 retrieves val1
K2 retrieves val2
K3 retrieves val3
K3, K31 retrieves val31
K3, K32, K321 retrieves val321
K3, K32, K322 retrieves val322
K3, K33 retrieves val33
K4 retrieves val4

Note: I want to allow for the possibility for not just primitives like string literals and numbers but objects to be inserted and looked up from the table. Since every non-primitive object is implemented using pairs, this makes it necessary for us to be able to distinguish between a sub-table and a value which is an object. So I am introducing tags so that the table procedures can correctly identify the data they handle as they traverse through the table. So a 'key' will not be just a string literal but a pair in which the 'car' is a tag with the value 'key and the 'cdr' is the actual key. Similarly, a 'value' will not just be a string literal but a pair in which the 'car' is a tag with the value 'value and the 'cdr' is the actual value which could be a primitive or a complex object.

The basic building block of this table is a key-value pair. It is a pair in which the 'car' is the key and the 'cdr' is another pair in which the 'car' is a pointer to a generic table and the 'cdr' is the value. This structure allows us to create an arbitrary number of levels in the table. If it is just a simple key-value pair with no sub-keys, then the contained generic table will be null. If it is a key with sub-keys but no value for this key, then the contained generic table will be non-null and the value will be null. And of course, both the contained generic table and the value can be non-null to allow for data like:

K3, K31: val31
K3, K32, K321: val321

And the structure of this generic table with an arbitrary
number of levels is:

(mlist '*generic-table* KVP1 KVP2 ....)

where KVP stands for key-value pair.

The logic for inserting a key list and its associated value:

Look for the first key from the key-list in the first level of the table. If found, then look for the 2nd key in the sub-table pointed to by the first key and continue this process. If all the keys are found, then the insert operation will replace the existing value with the new value. If at any level the key is not found, then insert that key at that level in the table and continue inserting keys below it and finally the value.

As with the other exercises, I have implemented a print-table procedure for printing the entire table (with indentations that signify the level of the key) in an easy-to-read manner.

See this diagram:


Comments

Popular posts from this blog

SICP Exercise 2.56 differentiation rule

SICP Exercise 1.28 (Miller-Rabin Test)

SICP Exercise 4.18 a alternative strategy for interpreting internal definitions