Blame


1 665c255d 2023-08-04 jrmu (define (add-complex z1 z2)
2 665c255d 2023-08-04 jrmu (make-from-real-imag (+ (real-part z1) (real-part z2))
3 665c255d 2023-08-04 jrmu (+ (imag-part z1) (imag-part z2))))
4 665c255d 2023-08-04 jrmu (define (sub-complex z1 z2)
5 665c255d 2023-08-04 jrmu (make-from-real-imag (- (real-part z1) (real-part z2))
6 665c255d 2023-08-04 jrmu (- (imag-part z1) (imag-part z2))))
7 665c255d 2023-08-04 jrmu (define (mul-complex z1 z2)
8 665c255d 2023-08-04 jrmu (make-from-mag-ang (* (magnitude z1) (magnitude z2))
9 665c255d 2023-08-04 jrmu (+ (angle z1) (angle z2))))
10 665c255d 2023-08-04 jrmu (define (div-complex z1 z2)
11 665c255d 2023-08-04 jrmu (make-from-mag-ang (/ (magnitude z1) (magnitude z2))
12 665c255d 2023-08-04 jrmu (- (angle z1) (angle z2))))
13 665c255d 2023-08-04 jrmu
14 665c255d 2023-08-04 jrmu (define (attach-tag type-tag contents)
15 665c255d 2023-08-04 jrmu (cons type-tag contents))
16 665c255d 2023-08-04 jrmu (define (type-tag datum)
17 665c255d 2023-08-04 jrmu (if (pair? datum)
18 665c255d 2023-08-04 jrmu (car datum)
19 665c255d 2023-08-04 jrmu (error "Bad tagged datum -- TYPE-TAG" datum)))
20 665c255d 2023-08-04 jrmu (define (contents datum)
21 665c255d 2023-08-04 jrmu (if (pair? datum)
22 665c255d 2023-08-04 jrmu (cdr datum)
23 665c255d 2023-08-04 jrmu (error "Bad tagged datum -- CONTENTS" datum)))
24 665c255d 2023-08-04 jrmu (define (rectangular? z)
25 665c255d 2023-08-04 jrmu (eq? (type-tag z) 'rectangular))
26 665c255d 2023-08-04 jrmu (define (polar? z)
27 665c255d 2023-08-04 jrmu (eq? (type-tag z) 'polar))
28 665c255d 2023-08-04 jrmu
29 665c255d 2023-08-04 jrmu (define (install-rectangular-package)
30 665c255d 2023-08-04 jrmu (define (real-part z) (car z))
31 665c255d 2023-08-04 jrmu (define (imag-part z) (cdr z))
32 665c255d 2023-08-04 jrmu (define (make-from-real-imag x y)
33 665c255d 2023-08-04 jrmu (cons x y))
34 665c255d 2023-08-04 jrmu (define (magnitude z)
35 665c255d 2023-08-04 jrmu (sqrt (+ (square (real-part z))
36 665c255d 2023-08-04 jrmu (square (imag-part z)))))
37 665c255d 2023-08-04 jrmu (define (angle z)
38 665c255d 2023-08-04 jrmu (atan (imag-part z) (real-part z)))
39 665c255d 2023-08-04 jrmu (define (make-from-mag-ang r a)
40 665c255d 2023-08-04 jrmu (cons (* r (cos a)) (* r (sin a))))
41 665c255d 2023-08-04 jrmu (define (tag x) (attach-tag 'rectangular x))
42 665c255d 2023-08-04 jrmu (put 'real-part '(rectangular) real-part)
43 665c255d 2023-08-04 jrmu (put 'imag-part '(rectangular) imag-part)
44 665c255d 2023-08-04 jrmu (put 'magnitude '(rectangular) magnitude)
45 665c255d 2023-08-04 jrmu (put 'angle '(rectangular) angle)
46 665c255d 2023-08-04 jrmu (put 'make-from-real-imag 'rectangular
47 665c255d 2023-08-04 jrmu (lambda (x y) (tag (make-from-real-imag x y))))
48 665c255d 2023-08-04 jrmu (put 'make-from-mag-ang 'rectangular
49 665c255d 2023-08-04 jrmu (lambda (r a) (tag (make-from-mag-ang r a))))
50 665c255d 2023-08-04 jrmu 'done)
51 665c255d 2023-08-04 jrmu
52 665c255d 2023-08-04 jrmu (define (install-polar-package)
53 665c255d 2023-08-04 jrmu (define (magnitude z) (car z))
54 665c255d 2023-08-04 jrmu (define (angle z) (cdr z))
55 665c255d 2023-08-04 jrmu (define (make-from-mag-ang r a) (cons r a))
56 665c255d 2023-08-04 jrmu (define (real-part z)
57 665c255d 2023-08-04 jrmu (* (magnitude z) (cos (angle z))))
58 665c255d 2023-08-04 jrmu (define (imag-part z)
59 665c255d 2023-08-04 jrmu (* (magnitude z) (sin (angle z))))
60 665c255d 2023-08-04 jrmu (define (make-from-real-imag x y)
61 665c255d 2023-08-04 jrmu (cons (sqrt (+ (square x) (square y)))
62 665c255d 2023-08-04 jrmu (atan y x)))
63 665c255d 2023-08-04 jrmu (define (tag x) (attach-tag 'polar x))
64 665c255d 2023-08-04 jrmu (put 'real-part '(polar) real-part)
65 665c255d 2023-08-04 jrmu (put 'imag-part '(polar) imag-part)
66 665c255d 2023-08-04 jrmu (put 'magnitude '(polar) magnitude)
67 665c255d 2023-08-04 jrmu (put 'angle '(polar) angle)
68 665c255d 2023-08-04 jrmu (put 'make-from-real-imag 'polar
69 665c255d 2023-08-04 jrmu (lambda (x y) (tag (make-from-real-imag x y))))
70 665c255d 2023-08-04 jrmu (put 'make-from-mag-ang 'polar
71 665c255d 2023-08-04 jrmu (lambda (r a) (tag (make-from-mag-ang r a))))
72 665c255d 2023-08-04 jrmu 'done)
73 665c255d 2023-08-04 jrmu
74 665c255d 2023-08-04 jrmu (define (apply-generic op . args)
75 665c255d 2023-08-04 jrmu (let* ((type-tags (map type-tag args))
76 665c255d 2023-08-04 jrmu (proc (get op type-tags)))
77 665c255d 2023-08-04 jrmu (if proc
78 665c255d 2023-08-04 jrmu (apply proc (map contents args))
79 665c255d 2023-08-04 jrmu (error
80 665c255d 2023-08-04 jrmu "No method for these types -- APPLY-GENERIC"
81 665c255d 2023-08-04 jrmu (list op type-tags)))))
82 665c255d 2023-08-04 jrmu
83 665c255d 2023-08-04 jrmu (define (real-part z) (apply-generic 'real-part z))
84 665c255d 2023-08-04 jrmu (define (imag-part z) (apply-generic 'imag-part z))
85 665c255d 2023-08-04 jrmu (define (magnitude z) (apply-generic 'magnitude z))
86 665c255d 2023-08-04 jrmu (define (angle z) (apply-generic 'angle z))
87 665c255d 2023-08-04 jrmu (define (make-from-real-imag x y)
88 665c255d 2023-08-04 jrmu ((get 'make-from-real-imag 'rectangular) x y))
89 665c255d 2023-08-04 jrmu (define (make-from-mag-ang r a)
90 665c255d 2023-08-04 jrmu : ((get 'make-from-mag-ang 'polar) r a))
91 665c255d 2023-08-04 jrmu
92 665c255d 2023-08-04 jrmu ;; 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.
93 665c255d 2023-08-04 jrmu
94 665c255d 2023-08-04 jrmu ;; 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:
95 665c255d 2023-08-04 jrmu
96 665c255d 2023-08-04 jrmu ;; 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?
97 665c255d 2023-08-04 jrmu
98 665c255d 2023-08-04 jrmu ;; 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?
99 665c255d 2023-08-04 jrmu
100 665c255d 2023-08-04 jrmu ;; 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.
101 665c255d 2023-08-04 jrmu
102 665c255d 2023-08-04 jrmu ;; 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?