22 hours ago
my attempt to do the exercises in sicp.
Wednesday, August 13, 2008
sicp exercise 2.84
;; Exercise 2.84. Using the raise operation of exercise 2.83, modify the apply-generic procedure so that it coerces its arguments to have the same type by the method of successive raising, as discussed in this section. You will need to devise a way to test which of two types is higher in the tower. Do this in a manner that is ``compatible'' with the rest of the system and will not lead to problems in adding new levels to the tower.
;; 1. get the highest type from the list of data types
;; 2. make a list of the highest type
;; 3. find the operation for that list of types
;; 4. if operation is found, prepare list of raised arguments
;; 5. apply the operation
(define (get-highest-type args)
(define (higher-type? num1 num2)
(let ((type1 (type num1))
(type2 (type num2)))
(if (eq? type1 type2)
num2
(let ((raise-proc (get 'raise (list type1))))
(if (null? raise-proc)
num1
(higher-type? (raise-proc (contents num1)) num2))))))
(define (iter num rest-num)
(cond ((null? rest-num) num)
(else (iter (higher-type? num (car rest-num))
(cdr rest-num)))))
(cond ((null? args) (error "ERROR"))
((null? (cdr args)) (car args))
(else (type (iter (car args) (cdr args))))))
(define (raise-args highest-type args)
(define (raise-to-type num)
(if (eq? (type num) highest-type)
num
(raise-to-type (raise num))))
(map raise-to-type args))
(define (apply-generic op . param)
(define (apply-generic-raised)
(let ((highest-type (get-highest-type param)))
(let ((highest-type-args (map (lambda(x) highest-type) param)))
(let ((proc (get op highest-type-args)))
(if (null? proc) (error "no matching operation for types"
highest-type-args)
(apply proc (map contents (raise-args highest-type param))))))))
(let ((type-tags (map type param)))
(let ((proc (get op type-tags)))
(if (not (null? proc)) (apply proc (map contents param))
(apply-generic-raised)))))
;;
;; testing
;;
(define (square x) (* x x))
(define *op-table* (make-hash-table 10))
(define (put op type proc)
(hash-set! *op-table* (list op type) proc))
(define (get op type)
(hash-ref *op-table* (list op type) '()))
(define (attach-tag tag oper)
(if (number? oper) oper
(list tag oper)))
(define (type data)
(if (number? data) 'scheme-number
(car data)))
(define (contents data)
(if (number? data) data
(cadr data)))
(define (add x y) (apply-generic 'add x y))
(define (sub x y) (apply-generic 'sub x y))
(define (mul x y) (apply-generic 'mul x y))
(define (div x y) (apply-generic 'div x y))
(define (=zero? x) (apply-generic '=zero? x))
(define (raise x) (apply-generic 'raise x))
(define (install-scheme-number-package)
(define (tag x)
(attach-tag 'scheme-number x))
(put 'add '(scheme-number scheme-number)
(lambda (x y) (tag (+ x y))))
(put 'sub '(scheme-number scheme-number)
(lambda (x y) (tag (- x y))))
(put 'mul '(scheme-number scheme-number)
(lambda (x y) (tag (* x y))))
(put 'div '(scheme-number scheme-number)
(lambda (x y) (tag (/ x y))))
(put 'make 'scheme-number
(lambda (x) (tag x)))
(put 'eq? '(scheme-number scheme-number)
(lambda(x y)(= x y)))
(put '=zero? '(scheme-number)
(lambda(x)(= x 0)))
(put 'raise '(scheme-number)
(lambda(x)(make-rational x 1)))
'done)
(define (make-scheme-number n)
((get 'make 'scheme-number) n))
(define (install-rational-package)
;; internal procedures
(define (numer x) (car x))
(define (denom x) (cdr x))
(define (make-rat n d)
(let ((g (gcd n d)))
(cons (/ n g) (/ d g))))
(define (add-rat x y)
(make-rat (+ (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (sub-rat x y)
(make-rat (- (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (mul-rat x y)
(make-rat (* (numer x) (numer y))
(* (denom x) (denom y))))
(define (div-rat x y)
(make-rat (* (numer x) (denom y))
(* (denom x) (numer y))))
(define (eq? x y)
(and (= (numer x) (numer y))
(= (denom x) (denom y))))
(define (=zero? x)
(= (numer x) 0))
(define (raise x)
(make-complex-from-real-imag (/ (numer x) (denom x)) 0))
;; interface to rest of the system
(define (tag x) (attach-tag 'rational x))
(put 'add '(rational rational)
(lambda (x y) (tag (add-rat x y))))
(put 'sub '(rational rational)
(lambda (x y) (tag (sub-rat x y))))
(put 'mul '(rational rational)
(lambda (x y) (tag (mul-rat x y))))
(put 'div '(rational rational)
(lambda (x y) (tag (div-rat x y))))
(put 'eq? '(rational rational) eq?)
(put '=zero? '(rational) =zero?)
(put 'raise '(rational) raise)
(put 'make 'rational
(lambda (n d) (tag (make-rat n d))))
'done)
(define (make-rational n d)
((get 'make 'rational) n d))
(define (install-rectangular-package)
;; internal procedures
(define (real-part z) (car z))
(define (imag-part z) (cdr z))
(define (make-from-real-imag x y) (cons x y))
(define (magnitude z)
(sqrt (+ (square (real-part z))
(square (imag-part z)))))
(define (angle z)
(atan (imag-part z) (real-part z)))
(define (make-from-mag-ang r a)
(cons (* r (cos a)) (* r (sin a))))
(define (eq? z1 z2)
(and (= (real-part z1) (real-part z2))
(= (imag-part z1) (imag-part z2))))
(define (=zero? z)
(and (= (real-part z) 0)
(= (imag-part z) 0)))
;; interface to the rest of the system
(define (tag x) (attach-tag 'rectangular x))
(put 'real-part '(rectangular) real-part)
(put 'imag-part '(rectangular) imag-part)
(put 'magnitude '(rectangular) magnitude)
(put 'angle '(rectangular) angle)
(put 'make-from-real-imag 'rectangular
(lambda (x y) (tag (make-from-real-imag x y))))
(put 'make-from-mag-ang 'rectangular
(lambda (r a) (tag (make-from-mag-ang r a))))
(put 'eq? '(rectangular rectangular) eq?)
(put '=zero? '(rectangular) =zero?)
'done)
(define (real-part z)
(apply-generic 'real-part z))
(define (imag-part z)
(apply-generic 'imag-part z))
(define (install-polar-package)
;; internal procedures
(define (magnitude z) (car z))
(define (angle z) (cdr z))
(define (make-from-mag-ang r a) (cons r a))
(define (real-part z)
(* (magnitude z) (cos (angle z))))
(define (imag-part z)
(* (magnitude z) (sin (angle z))))
(define (make-from-real-imag x y)
(cons (sqrt (+ (square x) (square y)))
(atan y x)))
(define (eq? z1 z2)
(and (= (magnitude z1) (magnitude z2))
(= (angle z1) (angle z2))))
(define (=zero? z)
(= (magnitude z) 0))
;; interface to the rest of the system
(define (tag x) (attach-tag 'polar x))
(put 'real-part '(polar) real-part)
(put 'imag-part '(polar) imag-part)
(put 'magnitude '(polar) magnitude)
(put 'angle '(polar) angle)
(put 'make-from-real-imag 'polar
(lambda (x y) (tag (make-from-real-imag x y))))
(put 'make-from-mag-ang 'polar
(lambda (r a) (tag (make-from-mag-ang r a))))
(put 'eq? '(polar polar) eq?)
(put '=zero? '(polar) =zero?)
'done)
(define (magnitude z)
(apply-generic 'magnitude z))
(define (install-complex-package)
;; imported procedures from rectangular and polar packages
(define (make-from-real-imag x y)
((get 'make-from-real-imag 'rectangular) x y))
;; internal procedures
(define (add-complex z1 z2)
(make-from-real-imag (+ (real-part z1) (real-part z2))
(+ (imag-part z1) (imag-part z2))))
(define (sub-complex z1 z2)
(make-from-real-imag (- (real-part z1) (real-part z2))
(- (imag-part z1) (imag-part z2))))
(define (mul-complex z1 z2)
(make-from-mag-ang (* (magnitude z1) (magnitude z2))
(+ (angle z1) (angle z2))))
(define (div-complex z1 z2)
(make-from-mag-ang (/ (magnitude z1) (magnitude z2))
(- (angle z1) (angle z2))))
;; interface to rest of the system
(define (tag z) (attach-tag 'complex z))
(put 'add '(complex complex)
(lambda (z1 z2) (tag (add-complex z1 z2))))
(put 'sub '(complex complex)
(lambda (z1 z2) (tag (sub-complex z1 z2))))
(put 'mul '(complex complex)
(lambda (z1 z2) (tag (mul-complex z1 z2))))
(put 'div '(complex complex)
(lambda (z1 z2) (tag (div-complex z1 z2))))
(put 'make-from-real-imag 'complex
(lambda (x y) (tag (make-from-real-imag x y))))
(put 'eq? '(complex complex) eq?)
(put '=zero? '(complex) =zero?)
(put 'real-part '(complex) real-part)
(put 'imag-part '(complex) imag-part)
(put 'magnitude '(complex) magnitude)
(put 'angle '(complex) angle)
'done)
(define (make-complex-from-real-imag x y)
((get 'make-from-real-imag 'complex) x y))
(install-scheme-number-package)
(install-rational-package)
(install-rectangular-package)
(install-complex-package)
(define num (make-scheme-number 23))
(define rat-num (raise 34))
(define cmplx-num (raise rat-num))
(define c1 (make-complex-from-real-imag 2 3))
(define c2 (make-complex-from-real-imag 6 7))
(display (add c1 c2)) (newline)
(display (add c1 num)) (newline)
(display (add num c1)) (newline)
(display (add c1 rat-num)) (newline)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment