Advertisement
Guest User

Untitled

a guest
Aug 8th, 2010
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 12.01 KB | None | 0 0
  1. #lang planet neil/sicp
  2.  
  3. ;; Ex 2.81 a
  4. ;;
  5. ; If Louis puts the coercion functions into the operations table, apply-generic will enter an infinite loop
  6. ; The logic is:
  7. ; if the desired operation is not found on types t1 and t2,
  8. ;        find the coerce-t1->t2 and coerce-t2->t1
  9. ;        call apply-generic using new argument created using the coerce procedures
  10. ; but when the t1 and t2 are the same type thye call to apply-generic will be using the same
  11. ; arguments as the initial call and so it will fail again and loop indefinitely
  12.  
  13. ;; Ex 2.81 b
  14. ;;
  15. ;It works correctly but if the table doesn't have the necessary procedure it will fail as expected.
  16.  
  17. ;; Ex 2.81 c
  18. ;;
  19.  
  20. ;; ======================================================================
  21. ;;
  22. ;; To test the exercises I need an implementation of put and get.
  23. ;; These are taken directly from section 3.3.3 of the book
  24. ;; http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-22.html#%_sec_3.3.3
  25. ;;
  26. ;; ======================================================================
  27. (define (make-table)
  28.   (let ((local-table (list '*table*)))
  29.     (define (lookup key-1 key-2)
  30.       (let ((subtable (assoc key-1 (cdr local-table))))
  31.         (if subtable
  32.             (let ((record (assoc key-2 (cdr subtable))))
  33.               (if record
  34.                   (cdr record)
  35.                   false))
  36.             false)))
  37.     (define (insert! key-1 key-2 value)
  38.       (let ((subtable (assoc key-1 (cdr local-table))))
  39.         (if subtable
  40.             (let ((record (assoc key-2 (cdr subtable))))
  41.               (if record
  42.                   (set-cdr! record value)
  43.                   (set-cdr! subtable
  44.                             (cons (cons key-2 value)
  45.                                   (cdr subtable)))))
  46.             (set-cdr! local-table
  47.                       (cons (list key-1
  48.                                   (cons key-2 value))
  49.                             (cdr local-table)))))
  50.       'ok)    
  51.     (define (dispatch m)
  52.       (cond ((eq? m 'lookup-proc) lookup)
  53.             ((eq? m 'insert-proc!) insert!)
  54.             ((eq? m 'table) local-table)
  55.             (else (error "Unknown operation -- TABLE" m))))
  56.     dispatch))
  57.  
  58. (define operation-table (make-table))
  59. (define get (operation-table 'lookup-proc))
  60. (define put (operation-table 'insert-proc!))
  61.  
  62. (define coercion-table (make-table))
  63. (define get-coercion (operation-table 'lookup-proc))
  64. (define put-coercion (operation-table 'insert-proc!))
  65.  
  66. (define (square x) (* x x))
  67.  
  68. (define (attach-tag type-tag contents)
  69.   (if (eq? type-tag 'scheme-number)
  70.       contents
  71.       (cons type-tag contents)))
  72.  
  73. (define (type-tag datum)
  74.   (cond ((pair? datum) (car datum))
  75.         ((number? datum) 'scheme-number)
  76.         (else (error "Bad tagged datum -- TYPE-TAG" datum))))
  77.  
  78. (define (contents datum)
  79.   (cond ((pair? datum) (cdr datum))
  80.         ((number? datum) cdr datum)
  81.         (else (error "Bad tagged datum -- CONTENTS" datum))))
  82.  
  83.  
  84. (define (apply-generic op . args)
  85.  
  86.   (define (signal-error type-tags)
  87.     (error "No method for these types - APPLY-GENERIC"
  88.            (list op type-tags)))
  89.  
  90.   (define (coerce-types tags args)
  91.     (let ((type1 (car tags))
  92.           (type2 (cadr tags))
  93.           (a1 (car args))
  94.           (a2 (cadr args)))
  95.       (if (eq? type1 type2)
  96.           (signal-error tags)
  97.           (let ((t1->t2 (get-coercion type1 type2))
  98.                 (t2->t1 (get-coercion type2 type1)))
  99.             (cond (t1->t2 (apply-generic op (t1->t2 a1) a2))
  100.                   (t2->t1 (apply-generic op a1 (t2->t1 a2)))
  101.                   (else (signal-error tags)))))))
  102.  
  103.   (let* ((type-tags (map type-tag args))
  104.          (proc (get op type-tags)))
  105.     (cond  (proc (apply proc (map contents args)))
  106.            ((= (length args) 2) (coerce-types type-tags args))
  107.            (else (signal-error type-tags)))))
  108.  
  109. ;; ======================================================================
  110. ;;
  111. ;; The scheme number package
  112. ;;
  113. ;; ======================================================================
  114. (define (install-scheme-number-package)
  115.  
  116.   (define (scheme-number->complex n)
  117.     (make-complex-from-real-imag (contents n) 0))
  118.  
  119.   (define (tag x)
  120.     (attach-tag 'scheme-number x))    
  121.   (put 'add  '(scheme-number scheme-number) (lambda (x y) (tag (+ x y))))
  122.   (put 'sub  '(scheme-number scheme-number) (lambda (x y) (tag (- x y))))
  123.   (put 'mul  '(scheme-number scheme-number) (lambda (x y) (tag (* x y))))
  124.   (put 'div  '(scheme-number scheme-number) (lambda (x y) (tag (/ x y))))
  125.   (put 'equ? '(scheme-number scheme-number) =)
  126.   (put '=zero? '(scheme-number) zero?)
  127.   (put 'make 'scheme-number (lambda (x) (tag x)))
  128.  
  129.   (put-coercion 'scheme-number 'complex scheme-number->complex)
  130.  
  131.   'done)
  132.  
  133.  
  134. ;; ======================================================================
  135. ;;
  136. ;; The rectangular number package
  137. ;;
  138. ;; ======================================================================
  139. (define (install-rectangular-package)
  140.   ;; internal procedures
  141.   (define (real-part z) (car z))
  142.   (define (imag-part z) (cdr z))
  143.   (define (make-from-real-imag x y) (cons x y))
  144.   (define (magnitude z)
  145.     (sqrt (+ (square (real-part z))
  146.              (square (imag-part z)))))
  147.   (define (angle z)
  148.     (atan (imag-part z) (real-part z)))
  149.   (define (make-from-mag-ang r a)
  150.     (cons (* r (cos a)) (* r (sin a))))
  151.   ;; interface to the rest of the system
  152.   (define (tag x) (attach-tag 'rectangular x))
  153.   (put 'real-part '(rectangular) real-part)
  154.   (put 'imag-part '(rectangular) imag-part)
  155.   (put 'magnitude '(rectangular) magnitude)
  156.   (put 'angle '(rectangular) angle)
  157.   (put 'make-from-real-imag 'rectangular
  158.        (lambda (x y) (tag (make-from-real-imag x y))))
  159.   (put 'make-from-mag-ang 'rectangular
  160.        (lambda (r a) (tag (make-from-mag-ang r a))))
  161.   'done)
  162.  
  163. ;; ======================================================================
  164. ;;
  165. ;; The polar number package
  166. ;;
  167. ;; ======================================================================
  168. (define (install-polar-package)
  169.   ;; internal procedures
  170.   (define (magnitude z) (car z))
  171.   (define (angle z) (cdr z))
  172.   (define (make-from-mag-ang r a) (cons r a))
  173.   (define (real-part z)
  174.     (* (magnitude z) (cos (angle z))))
  175.   (define (imag-part z)
  176.     (* (magnitude z) (sin (angle z))))
  177.   (define (make-from-real-imag x y)
  178.     (cons (sqrt (+ (square x) (square y)))
  179.           (atan y x)))
  180.   ;; interface to the rest of the system
  181.   (define (tag x) (attach-tag 'polar x))
  182.   (put 'real-part '(polar) real-part)
  183.   (put 'imag-part '(polar) imag-part)
  184.   (put 'magnitude '(polar) magnitude)
  185.   (put 'angle '(polar) angle)
  186.   (put 'make-from-real-imag 'polar
  187.        (lambda (x y) (tag (make-from-real-imag x y))))
  188.   (put 'make-from-mag-ang 'polar
  189.        (lambda (r a) (tag (make-from-mag-ang r a))))
  190.   'done)
  191.  
  192.  
  193. ;; ======================================================================
  194. ;;
  195. ;; The rational number package
  196. ;;
  197. ;; ======================================================================
  198. (define (install-rational-package)
  199.   ;; internal procedures
  200.   (define (numer x) (car x))
  201.   (define (denom x) (cdr x))
  202.   (define (make-rat n d) (let ((g (gcd n d)))
  203.                            (cons (/ n g) (/ d g))))
  204.   (define (add-rat x y) (make-rat (+ (* (numer x) (denom y))
  205.                                      (* (numer y) (denom x)))
  206.                                   (* (denom x) (denom y))))
  207.   (define (sub-rat x y) (make-rat (- (* (numer x) (denom y))
  208.                                      (* (numer y) (denom x)))
  209.                                   (* (denom x) (denom y))))
  210.   (define (mul-rat x y) (make-rat (* (numer x) (numer y))
  211.                                   (* (denom x) (denom y))))
  212.   (define (div-rat x y) (make-rat (* (numer x) (denom y))
  213.                                   (* (denom x) (numer y))))
  214.   (define (equ-rat x y) (and (equ? (numer x) (numer y))
  215.                              (equ? (denom x) (denom y))))
  216.   (define (=zero? x) (zero? (numer x)))
  217.  
  218.   ;; interface to rest of the system
  219.   (define (tag x) (attach-tag 'rational x))
  220.   (put 'add '(rational rational)  (lambda (x y) (tag (add-rat x y))))
  221.   (put 'sub '(rational rational)  (lambda (x y) (tag (sub-rat x y))))
  222.   (put 'mul '(rational rational)  (lambda (x y) (tag (mul-rat x y))))
  223.   (put 'div '(rational rational)  (lambda (x y) (tag (div-rat x y))))
  224.   (put 'equ? '(rational rational) equ-rat)
  225.   (put '=zero? '(rational) =zero?)
  226.   (put 'make 'rational (lambda (n d) (tag (make-rat n d))))
  227.   'done)
  228.  
  229. ;; ======================================================================
  230. ;;
  231. ;; The complex number package
  232. ;;
  233. ;; ======================================================================
  234. (define (install-complex-package)
  235.   ;; imported procedures from rectangular and polar packages
  236.   (define (make-from-real-imag x y) ((get 'make-from-real-imag 'rectangular) x y))
  237.   (define (make-from-mag-ang r a) ((get 'make-from-mag-ang 'polar) r a))
  238.   ;; internal procedures
  239.   (define (add-complex z1 z2) (make-from-real-imag (+ (real-part z1) (real-part z2))
  240.                                                    (+ (imag-part z1) (imag-part z2))))
  241.   (define (sub-complex z1 z2) (make-from-real-imag (- (real-part z1) (real-part z2))
  242.                                                    (- (imag-part z1) (imag-part z2))))
  243.   (define (mul-complex z1 z2) (make-from-mag-ang (* (magnitude z1) (magnitude z2))
  244.                                                  (+ (angle z1) (angle z2))))
  245.   (define (div-complex z1 z2) (make-from-mag-ang (/ (magnitude z1) (magnitude z2))
  246.                                                  (- (angle z1) (angle z2))))
  247.   (define (equ-complex z1 z2) (and (equ? (magnitude z1) (magnitude z2))
  248.                                    (equ? (angle z1) (angle z2))))
  249.   (define (=zero? z1) (zero? (magnitude z1)))
  250.   ;; interface to rest of the system
  251.   (define (tag z) (attach-tag 'complex z))
  252.   (put 'add    '(complex complex) (lambda (z1 z2) (tag (add-complex z1 z2))))
  253.   (put 'sub    '(complex complex) (lambda (z1 z2) (tag (sub-complex z1 z2))))
  254.   (put 'mul    '(complex complex) (lambda (z1 z2) (tag (mul-complex z1 z2))))
  255.   (put 'div    '(complex complex) (lambda (z1 z2) (tag (div-complex z1 z2))))
  256.   (put 'equ?   '(complex complex) equ-complex)
  257.   (put '=zero? '(complex) =zero?)
  258.   (put 'make-from-real-imag 'complex (lambda (x y) (tag (make-from-real-imag x y))))
  259.   (put 'make-from-mag-ang 'complex (lambda (r a) (tag (make-from-mag-ang r a))))
  260.   (put 'real-part '(complex) real-part)
  261.   (put 'imag-part '(complex) imag-part)
  262.   (put 'magnitude '(complex) magnitude)
  263.   (put 'angle     '(complex) angle)
  264.   'done)
  265.  
  266. ;; ======================================================================
  267. ;;
  268. ;; Generic procedures
  269. ;;
  270. ;; ======================================================================
  271.  
  272. ; Constructors
  273. (define (make-scheme-number n)
  274.   ((get 'make 'scheme-number) n))
  275. (define (make-rational numer denom)
  276.   ((get 'make 'rational) numer denom))
  277. (define (make-complex-from-real-imag x y)
  278.   ((get 'make-from-real-imag 'complex) x y))
  279. (define (make-complex-from-mag-ang r a)
  280.   ((get 'make-from-mag-ang 'complex) r a))
  281.  
  282. (define (add x y) (apply-generic 'add x y))
  283. (define (sub x y) (apply-generic 'sub x y))
  284. (define (mul x y) (apply-generic 'mul x y))
  285. (define (div x y) (apply-generic 'div x y))
  286. (define (real-part z) (apply-generic 'real-part z))
  287. (define (imag-part z) (apply-generic 'imag-part z))
  288. (define (magnitude z) (apply-generic 'magnitude z))
  289. (define (angle     z) (apply-generic 'angle     z))
  290. (define (equ? x y) (apply-generic 'equ? x y))
  291. (define (=zero? x) (apply-generic '=zero? x))
  292.  
  293.  
  294. ;; ======================================================================
  295. ;;
  296. ;; Package installation
  297. ;;
  298. ;; ======================================================================
  299. (define (install-number-packages)
  300.   (install-scheme-number-package)
  301.   (install-polar-package)
  302.   (install-rectangular-package)
  303.   (install-rational-package)
  304.   (install-complex-package))  
  305.  
  306. (install-number-packages)
  307.  
  308. ;(add 3 5)
  309. ;; => 8
  310. ;(sub 5 12)
  311. ;; -7
  312. ;(mul 4 7)
  313. ;; => 28
  314. ;(div (make-scheme-number 30) 6)
  315. ;; => 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement