Blame


1 665c255d 2023-08-04 jrmu (define (make-from-real-imag x y)
2 665c255d 2023-08-04 jrmu (define (dispatch op)
3 665c255d 2023-08-04 jrmu (cond ((eq? op 'real-part) x)
4 665c255d 2023-08-04 jrmu ((eq? op 'imag-part) y)
5 665c255d 2023-08-04 jrmu ((eq? op 'magnitude)
6 665c255d 2023-08-04 jrmu (sqrt (+ (square x) (square y))))
7 665c255d 2023-08-04 jrmu ((eq? op 'angle) (atan y x))
8 665c255d 2023-08-04 jrmu (else (error "Unknown op -- MAKE-FROM-REAL-IMAG" op))))
9 665c255d 2023-08-04 jrmu dispatch)
10 665c255d 2023-08-04 jrmu
11 665c255d 2023-08-04 jrmu (define (apply-generic op arg) (arg op))
12 665c255d 2023-08-04 jrmu
13 665c255d 2023-08-04 jrmu ;; Exercise 2.75. Implement the constructor make-from-mag-ang in message-passing style. This procedure should be analogous to the make-from-real-imag procedure given above.
14 665c255d 2023-08-04 jrmu
15 665c255d 2023-08-04 jrmu (define (make-from-mag-ang r a)
16 665c255d 2023-08-04 jrmu (define (dispatch op)
17 665c255d 2023-08-04 jrmu (cond ((eq? op 'real-part) (* r (cos a)))
18 665c255d 2023-08-04 jrmu ((eq? op 'imag-part) (* r (sin a)))
19 665c255d 2023-08-04 jrmu ((eq? op 'magnitude) r)
20 665c255d 2023-08-04 jrmu ((eq? op 'angle) a)
21 665c255d 2023-08-04 jrmu (else (error "Unknown op -- MAKE-FROM-REAL-IMAG" op))))
22 665c255d 2023-08-04 jrmu dispatch)
23 665c255d 2023-08-04 jrmu
24 665c255d 2023-08-04 jrmu ;; Exercise 2.76. As a large system with generic operations evolves, new types of data objects or new operations may be needed. For each of the three strategies -- generic operations with explicit dispatch, data-directed style, and message-passing-style -- describe the changes that must be made to a system in order to add new types or new operations. Which organization would be most appropriate for a system in which new types must often be added? Which would be most appropriate for a system in which new operations must often be added?
25 665c255d 2023-08-04 jrmu
26 665c255d 2023-08-04 jrmu ;; For generic operations with explicit dispatch, you need to update every single one of the generic selectors each time you add a new data type. You'll also need to add a new constructor for that data type. If you add a new generic operation, all of the data types that want to support it will need to provide a procedure. The maintainer of the generic operation needs to know about all the data types in order to make a proper dispatch. Explicit dispatch is the most burdensome to maintain.
27 665c255d 2023-08-04 jrmu
28 665c255d 2023-08-04 jrmu ;; For data-directed and message-passing style, each time you add a new data type, there is no need for the generic operation to be updated. For data-directed style, the implementor of the new data type just needs to put the corresponding procedure into the table. For message-passing style, a table does not even need to be updated. The implementor only needs to update his data object to learn how to handle any messages the object needs to support.
29 665c255d 2023-08-04 jrmu
30 665c255d 2023-08-04 jrmu ;; When adding a new operation, for both cases, the implementor of the data type will need to add a new procedure (or learn how to respond to the new operation message) if the operation needs to be supported.