Blob


1 ;; Exercise 2.74. Insatiable Enterprises, Inc., is a highly decentralized conglomerate company consisting of a large number of independent divisions located all over the world. The company's computer facilities have just been interconnected by means of a clever network-interfacing scheme that makes the entire network appear to any user to be a single computer. Insatiable's president, in her first attempt to exploit the ability of the network to extract administrative information from division files, is dismayed to discover that, although all the division files have been implemented as data structures in Scheme, the particular data structure used varies from division to division. A meeting of division managers is hastily called to search for a strategy to integrate the files that will satisfy headquarters' needs while preserving the existing autonomy of the divisions.
3 ;; Show how such a strategy can be implemented with data-directed programming. As an example, suppose that each division's personnel records consist of a single file, which contains a set of records keyed on employees' names. The structure of the set varies from division to division. Furthermore, each employee's record is itself a set (structured differently from division to division) that contains information keyed under identifiers such as address and salary. In particular:
5 (define (apply-generic op . args)
6 (let* ((type-tags (map type-tag args))
7 (proc (get op type-tags)))
8 (if proc
9 (apply proc (map contents args))
10 (error "invalid operation/type"))))
12 (define (attach-tag type-tag contents)
13 (cons type-tag contents))
14 (define (type-tag datum)
15 (if (pair? datum)
16 (car datum)
17 (error "invalid datum")))
18 (define (contents datum)
19 (if (pair? datum)
20 (cdr datum)
21 (error "invalid datum")))
23 ;; returns name of given record
24 (define (name record)
25 (apply-generic 'name record))
26 (define (address record)
27 (apply-generic 'address record))
28 (define (salary record)
29 (apply-generic 'salary record))
30 (define (make-file1-record1 name salary address other)
31 ((get 'make-file1-record1 '(file1 record1)) name salary address other))
34 (define (install-file1)
35 (define (make-record1 name salary address other)
36 (list name salary address other))
37 (define (name-record1 record)
38 (car record))
39 ;; we'll implement the file as a simple unordered list
40 (define (in-file? file name)
41 (and (not (null? file))
42 (or (eq? (car file) name)
43 (cond ((null? file) #f)
44 ((eq? (car file) name) #t)
45 (else (in-file? (cdr file) name))))
46 (define
47 )
48 (put 'make-file1-record1
49 '(file1 record1)
50 (lambda (name salary address other)
51 (attach-tag '(file1 record1)
52 (make-record1 name salary address other))))
54 (put 'name '(file1 record1) ...)
58 ;; I should define an in-file? procedure and an add-record procedure instead
60 ;; (define (make-file-internal list-of-records)
61 ;; list-of-records)
62 ;; (put 'make-file 'file1 (lambda (list-of-records)
63 ;; (attach-tag 'file1 (make-file-interal list-of-records))))
64 ;; (define (make-file1 list-of-records)
65 ;; ((get 'make-file 'file1) list-of-records))
67 ;; a. Implement for headquarters a get-record procedure that retrieves a specified employee's record from a specified personnel file. The procedure should be applicable to any division's file. Explain how the individual divisions' files should be structured. In particular, what type information must be supplied?
69 ;; get specified record from specified file
70 (define (get-record file name)
71 (apply-generic 'get-record file name))
73 (define (get-record-file1 file name)
74 (cond ((null? file) (error "person not found" name))
75 ((eq? (car file) name) (car file))
76 (else (get-record-file1 (cdr file) name))))
77 (put 'get-record '(file1 file1-record) get-record-file1)
79 ;; b. Implement for headquarters a get-salary procedure that returns the salary information from a given employee's record from any division's personnel file. How should the record be structured in order to make this operation work?
81 ;; c. Implement for headquarters a find-employee-record procedure. This should search all the divisions' files for the record of a given employee and return the record. Assume that this procedure takes as arguments an employee's name and a list of all the divisions' files.
83 ;; d. When Insatiable takes over a new company, what changes must be made in order to incorporate the new personnel information into the central system?