3.25 Exercise 3.25
To generalize tables beyond one and two dimensional keys, we
can imagine that a table entry is either a table or a key.
When performing a lookup, we should expect to find a value
only after looking up every one of the keys we were supplied
with. If a key is not present or if a lookup evaluates to a
value before exhausting the given keys, a lookup is said to
have failed. If the list of keys is exhausted before finding
a value, I believe returning the subtable is the best thing
to do.
Because I am going to be returning tables in addition to
values, I believe it is better to define lookup and
insert as functions taking tables rather than
constructing the tables as objects containing these
methods. However, I am going to construct these methods
differently than we did previously, by making them take
advantage of partial application to make the use of these
functions easier when operating on the same table.
TODO: Look more carefully at this
(define (lookup table) | (lambda (keys) | (if (null? keys) table | (let ((record (assoc (car keys) (cdr table)))) | (if record | ((lookup record) (cdr keys)) | false))))) |
|
(define (insert! table) | (lambda (args) | (if (not (null? args)) | (if (null? (cdr args)) | (set-cdr! table (car args)) | (let ((subtable (assoc (car args) (cdr table)))) | (if subtable | ((insert! subtable) (cdr args)) | (let ((new-subtable (list (car args)))) | ((insert! new-subtable) (cdr args)) | (set-cdr! table (cons new-subtable (cdr table)))))))))) |
|