sicp/2_75.rkt

22 lines
842 B
Racket

#lang sicp
(define (make-from-mag-ang r theta)
(lambda (op)
(cond ((eq? op 'real-part) (* r (cos theta)))
((eq? op 'imag-part) (* r (sin theta)))
((eq? op 'magnitude) r)
((eq? op 'angle) theta)
(else (error "Unknown op: make-from-mag-ang" op)))))
;; Generic operations, explicit dispatch: to add a new operation, a new rule must be added
;; for each existing type. To add a new type, a new rule must be added for each existing
;; operation.
;;
;; Data directed: to add a new operation, a new entry is added to the table for each type.
;; To add a new type, a new entry is added to the table for each operation.
;;
;; Message passing; to add a new operation a new conditional case is added to each relevant
;; object. To add a new type, a new object is created with all required operations.