Guest User

generic operators

a guest
Jun 23rd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 4.07 KB | None | 0 0
  1. ;; Table abstraction
  2. (define dict-proc (make-hash-table))
  3. (define (put key value)
  4.     (hash-create-handle! dict-proc key value))
  5. (define (get key)
  6.     (hash-get-handle dict-proc key))
  7.  
  8. ;; Tags
  9. (define attach-tag cons)
  10. (define type car)
  11. (define contents cdr)
  12.  
  13. ;; ***********************************************
  14. ;; Complex numbers
  15. ;; ***********************************************
  16.  
  17. ;; Complex numbers constructor for rectangular and polar
  18.  
  19. (define (make-rect obj-x obj-y)
  20.     (attach-tag `rect (cons obj-x obj-y)))
  21. (define (make-polar obj-x obj-y)
  22.     (attach-tag `polar (cons obj-x obj-y)))
  23. (define (make-complex obj)
  24.     (attach-tag `complex obj))
  25.  
  26. ;; complex numbers rect selectors
  27. (define (complex-init-rect)
  28.     (define re-rect car)
  29.     (define im-rect cdr)
  30.     (define (mag-rect a)
  31.         (let ([x (re-rect a)]        
  32.               [y (im-rect a)]
  33.               [square (lambda (a) (* a a))])
  34.         (sqrt (+ (square x) (square y)))))
  35.     (define (ang-rect a)
  36.         (let ([x (re-rect a)]        
  37.               [y (im-rect a)])
  38.               (atan (/ x y))))
  39.     (put (cons `re `rect) re-rect)
  40.     (put (cons `im `rect) im-rect)
  41.     (put (cons `mag `rect) mag-rect)
  42.     (put (cons `ang `rect) ang-rect))
  43.  
  44. ;; complex numbers polar selectors                                                                                    
  45. (define (complex-init-polar)
  46.     (define mag-polar car)
  47.     (define ang-polar cdr)
  48.     (define (re-polar a)
  49.         (let ([r (mag-polar a)]        
  50.               [theta (ang-polar a)])
  51.         (* r (cos theta))))
  52.     (define (im-polar a)
  53.         (let ([r (mag-polar a)]        
  54.               [theta (ang-polar a)])
  55.         (* r (sin theta))))
  56.     (put (cons `re `polar) re-polar)
  57.     (put (cons `im `polar) im-polar)
  58.     (put (cons `mag `polar) mag-polar)
  59.     (put (cons `ang `polar) ang-polar))
  60.  
  61. (define (operate op obj)
  62.     (let ([proc (get (cons op (type obj)))])
  63.         (if (not (null? proc)) (proc (contents obj))
  64.             (error "ERROR: operation cannot be performed on object"))))
  65.  
  66. (define (operate-general op obj-x obj-y)
  67.     (if (eq? (type obj-x) (type obj-y))
  68.         (let ([proc (get (cons op (type obj-x)))])
  69.             (if (not (null? proc)) (proc (contents obj-x) (contents obj-y))
  70.                 (error "ERROR: No such operation is defined")))
  71.         (error "ERROR: The types do not match for the operation to occur")))
  72.  
  73. (define (re obj)
  74.     (operate `re obj))    
  75. (define (im obj)
  76.     (operate `im obj))
  77. (define (mag obj)
  78.     (operate `mag obj))
  79. (define (ang obj)
  80.     (operate `ang obj))
  81.  
  82. (define (+c obj-x obj-y)
  83.     (make-rect (+ (re obj-x) (re obj-y))
  84.                (+ (im obj-y) (im obj-y))))
  85. (define (-c obj-x obj-y)
  86.     (make-rect (- (re obj-x) (re obj-y))
  87.                (- (im obj-y) (im obj-y))))
  88. (define (*c obj-x obj-y)
  89.     (make-polar (* (mag obj-x) (mag obj-y))
  90.                 (+ (ang obj-x) (ang obj-y))))                                            
  91. (define (/c obj-x obj-y)
  92.     (make-polar (* (mag obj-x) (mag obj-y))
  93.                        (+ (ang obj-x) (ang obj-y))))
  94.  
  95. ;; Generic selectors
  96. (define (complex-init)
  97.     (define (+complex obj-x obj-y)
  98.         (make-complex (+c obj-x obj-y)))
  99.     (define (-complex obj-x obj-y)
  100.         (make-complex (-c obj-x obj-y)))
  101.     (define (*complex obj-x obj-y)
  102.         (make-complex (*c obj-x obj-y)))    
  103.     (define (/complex obj-x obj-y)
  104.         (make-complex (/c obj-x obj-y)))
  105.  
  106.     (put (cons `add `complex) +complex)
  107.     (put (cons `sub `complex) -complex)
  108.     (put (cons `mul `complex) *complex)
  109.     (put (cons `div `complex) /complex))
  110.  
  111. ;; Generic operations
  112. (define (add obj-x obj-y)
  113.     (operate-general 'add obj-x obj-y))
  114.  
  115. (define (sub obj-x obj-y)
  116.     (operate-general 'sub obj-x obj-y))
  117.  
  118. (define (mul obj-x obj-y)
  119.     (operate-general 'mul obj-x obj-y))
  120.  
  121. (define (div obj-x obj-y)
  122.     (operate-general 'div obj-x obj-y))
  123.  
  124. ;; Driver program
  125. (complex-init-rect)
  126. (complex-init-polar)
  127. (complex-init)
  128. (define a (make-complex (make-rect 2 2)))
  129. (define b (make-complex (make-rect 1 3)))
  130.  
  131. (format #t "~a\n" (add a b))
Add Comment
Please, Sign In to add comment