Guest User

Untitled

a guest
Apr 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. Complex numbers III
  2.  
  3. Data-directed programming
  4.  
  5. This solves two problems:
  6.  
  7. 1. The constructor and selector functions had to know both
  8. representations in order to decide what to do.
  9.  
  10. 2. The underlying names had to be disambiguated. So for instance,
  11. we needed
  12.  
  13. real-part-rectangular
  14. real-part-polar
  15.  
  16. both defined in order to finally define real-part in terms of
  17. them.
  18.  
  19. With data-directed programming, neither of these problems occurs.
  20.  
  21. (define (square t) (* t t))
  22. (define (atan2 y x)(atan (/ y x))) ;; UMB Scheme doesn't have (atan y x)
  23.  
  24. (define (install-rectangular-package)
  25. ;; internal procedures
  26. (define (real-part z) (car z))
  27. (define (imag-part z) (cdr z))
  28. (define (make-from-real-imag x y) (cons x y))
  29. (define (magnitude z)
  30. (sqrt (+ (square (real-part z))
  31. (square (imag-part z)))))
  32. (define (angle z)
  33. (atan2 (imag-part z) (real-part z)))
  34. (define (make-from-mag-ang r a)
  35. (cons (* r (cos a)) (* r (sin a))))
  36.  
  37. ;; interface to the rest of the system
  38. (define (tag x) (attach-tag 'rectangular x))
  39. (put 'real-part 'rectangular real-part)
  40. (put 'imag-part 'rectangular imag-part)
  41. (put 'magnitude 'rectangular magnitude)
  42. (put 'angle 'rectangular angle)
  43. (put 'make-from-real-imag 'rectangular
  44. (lambda (x y) (tag (make-from-real-imag x y))))
  45. (put 'make-from-mag-ang 'rectangular
  46. (lambda (r a) (tag (make-from-mag-ang r a))))
  47. 'done)
  48.  
  49. (define (install-polar-package)
  50. ;; internal procedures
  51. (define (magnitude z) (car z))
  52. (define (angle z) (cdr z))
  53. (define (make-from-mag-ang r a) (cons r a))
  54. (define (real-part z)
  55. (* (magnitude z) (cos (angle z))))
  56. (define (imag-part z)
  57. (* (magnitude z) (sin (angle z))))
  58. (define (make-from-real-imag x y)
  59. (cons (sqrt (+ (square x) (square y)))
  60. (atan2 y x)))
  61.  
  62. ;; interface to the rest of the system
  63. (define (tag x) (attach-tag 'polar x))
  64. (put 'real-part 'polar real-part)
  65. (put 'imag-part 'polar imag-part)
  66. (put 'magnitude 'polar magnitude)
  67. (put 'angle 'polar angle)
  68. (put 'make-from-real-imag 'polar
  69. (lambda (x y) (tag (make-from-real-imag x y))))
  70. (put 'make-from-mag-ang 'polar
  71. (lambda (r a) (tag (make-from-mag-ang r a))))
  72. 'done)
  73.  
  74.  
  75. (define (apply-generic op arg)
  76. (let ((proc (get op (type-tag arg))))
  77. (if proc
  78. (proc (contents arg))
  79. (error
  80. "No method for this type -- APPLY-GENERIC"
  81. (list op (type-tag arg))))))
  82.  
  83. ;; Generic selectors
  84.  
  85. (define (real-part z) (apply-generic 'real-part z))
  86. (define (imag-part z) (apply-generic 'imag-part z))
  87. (define (magnitude z) (apply-generic 'magnitude z))
  88. (define (angle z) (apply-generic 'angle z))
  89.  
  90.  
  91. ;; Constructors for complex numbers
  92.  
  93. (define (make-from-real-imag x y)
  94. ((get 'make-from-real-imag 'rectangular) x y))
  95.  
  96. (define (make-from-mag-ang r a)
  97. ((get 'make-from-mag-ang 'polar) r a))
  98.  
  99.  
  100. ;; ****************** MESSAGE PASSING ******************
  101.  
  102. (define (make-from-real-imag x y)
  103. (define (dispatch op)
  104. (cond ((eq? op 'real-part) x)
  105. ((eq? op 'imag-part) y)
  106. ((eq? op 'magnitude)
  107. (sqrt (+ (square x) (square y))))
  108. ((eq? op 'angle) (atan2 y x))
  109. (else
  110. (error "Unknown op -- MAKE-FROM-REAL-IMAG" op))))
  111. dispatch)
  112.  
  113. (define (apply-generic op arg) (arg op))
Add Comment
Please, Sign In to add comment