sicp-solutions/chapter-2/ex-2.74.txt
2025-10-01 15:46:25 +02:00

59 lines
2.6 KiB
Text

This is a txt file because there's no sense in trying to make it into a proper
runnable scheme file, unless I go out of my way to make several examples of
data structure and record formats, complete with examples in order to actually
run these:
a) Before any of the data records in the file itself, there should be an
identifier for what type of record it is, so each file should start with a
symbol, number or string that uniquely identifies the file structure/format,
for example:
european-division-format
(...)
(...)
...
This piece of info should be in the same position in every file, so that a
uniform function, say (get-file-type file) could be used to find the proper
format. If it's not possible to change these division formats at all, then
either this change will be done while loading the file, or, the get-file-type
function will have to be more complicated.
In any case, once we have solved the format marking issue using any kind of
method, the files and the records could be structured any how, as long as each
record has a unique identifier to the employee.
These can then be searched with different functions for different formats, all
of which could be organized in a table, and called by a master function, which
will load the record, and then attach it with a tag, which will make record
processing easier too:
(define (get-record name file)
(let ((file-type (get-file-type file)))
(attach-record-type file-type
((get 'get-record file-type) name))))
We will also assume all these functions return nil if the record doesn't
exist, or a single record if it does.
b) The way that we've written the get-record function, it doesn't matter how a
particular employee record is structured, because we tag it as the respective
file type ourselves, but of course it must contain the salary field, we must
also define a function which gets the record type, and the rest of the record:
get-record-type and get-record-body
(define (get-salary record)
((get 'get-salary (get-record-type record)) (get-record-body record)))
c) We assume there's only one employee for the given name
(define (find-employee-record name files)
(let ((returns (filter (lambda (record) (not (null? record)))
(map get-record files))))
(if (null? returns)
nil
(car returns))))
d) All changes could be done in the new company's (or department's) package,
where they would simply define a new format name, add the format name tag to
their records, modify their get-record and get-salary functions appropriately,
and then add these to the updated global function table.