#lang sicp ; Building on top of exercise 2.83-84 ; With lots of changes, from here on, I'm splitting the scheme-number type into ; two separate types, integer and real. Both will be marked with a preceding ; symbol. ; We will assume that the scheme-internal "rational?" type *will not be used at ; all* ;;; ;;; ;;; Setup for the exercise ;;; ;;; (define (square x) (mul x x)) ; Dispatch table: list of rows, structure below: ; one row: (op-tag . (... one cell) (...) (...)) ; one cell: ((list of type tags) . procedure) ; essentially, the whole table is an alist of alists ; (list of type tags) is a list of length n for n-ary op ; a unary op can be called with a symbol instead of a list of symbols for a type ; tag, but this will be converted to a length-1 list while searching the table (define *dispatch-table* '()) ;;; ;;; Dispatch table ;;; ; finds member in alist based on tag, which is the car of the row (define (search-in-list tag list) (if (null? list) nil (let* ((current-member (car list)) (member-tag (car current-member))) (if (equal? member-tag tag) current-member ; we don't strip the key when returning (search-in-list tag (cdr list)))))) (define (get op-tag init-type-tags) (let* ((type-tags (if (symbol? init-type-tags) (list init-type-tags) init-type-tags)) ; wrap type-tags in a list if it's supplied as a symbol (row-1 (search-in-list op-tag *dispatch-table*)) (row (if (null? row-1) nil (cdr row-1))) ; we strip the name of the row (procedure-1 (search-in-list type-tags row)) (procedure (if (null? procedure-1) nil (cdr procedure-1)))) (if (null? procedure) #f procedure))) (define (add-pair! alist tag value) (set! alist (cons (cons tag value) alist))) (define (put op-tag init-type-tags procedure) (let* ((type-tags (if (symbol? init-type-tags) (list init-type-tags) init-type-tags)) (search-row (search-in-list op-tag *dispatch-table*)) ; here we don't strip the key from search-in-list (row (if (null? search-row) (begin (set! *dispatch-table* (cons (cons op-tag nil) *dispatch-table*)) (car *dispatch-table*)) search-row)) (search-cell (search-in-list type-tags (cdr row)))) (if (null? search-cell) (set-cdr! row (cons (cons type-tags procedure) (cdr row))) ; attaching new pair to row (set-cdr! search-cell procedure)))) ;;; ;;; Auxillary functions for type tags ;;; ; The actual exercise 2.78 (define (attach-tag type-tag contents) (cons type-tag contents)) (define (type-tag datum) (if (pair? datum) (car datum) (error "Bad tagged datum: TYPE-TAG" datum))) (define (contents datum) (if (pair? datum) (cdr datum) (error "Bad tagged datum: CONTENTS" datum))) ;;; ;;; Complex packages ;;; (define (prohibit-complex-number x func-name) (if (and (pair? x) (memq (type-tag x) '(complex rectangular polar))) (error "Complex arguments prohibited in constructor: " func-name x))) (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) (prohibit-complex-number x) (prohibit-complex-number y) (cons (tag-scheme-number x) (tag-scheme-number y))) (define (magnitude z) (square-root (add (square (real-part z)) (square (imag-part z))))) (define (angle z) (arctangent (imag-part z) (real-part z))) (define (make-from-mag-ang r a) (prohibit-complex-number r) (prohibit-complex-number a) (cons (mul r (cosine a)) (mul r (sine a)))) ;; 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)))) 'done) (define (install-polar-package) ;; internal procedures (define (magnitude z) (car z)) (define (angle z) (cdr z)) (define (make-from-mag-ang r a) (prohibit-complex-number r) (prohibit-complex-number 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) (prohibit-complex-number x) (prohibit-complex-number y) (cons (sqrt (+ (square x) (square y))) (atan y x))) ;; 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)))) 'done) (install-rectangular-package) (install-polar-package) ;;; ;;; Apply generic and complex function definitions ;;; (define (apply-generic op . args) (let ((type-tags (map type-tag args))) (let ((proc (get op type-tags))) (let ((result (if proc (apply proc (map contents args)) (if (= (length args) 2) (let ((type1 (car type-tags)) (type2 (cadr type-tags)) (a1 (car args)) (a2 (cadr args))) (cond ((supertype type1 type2) (apply-generic op a1 (raise a2))) ((supertype type2 type1) (apply-generic op (raise a1) a2)) (else (error "No method for these types" (list op type-tags))))) (error "No method for these types" (list op type-tags)))))) (if (and (pair? result) (memq (type-tag result) '(integer rational real complex)) ; remember, apply-generic can return objects which are not in our ; type system at all, so we have to check for that before we drop ; the result (not (memq op '(raise project)))) ; also, we don't want a call to project or raise to call drop, which ; calls them both (drop result) result))))) (define (real-part z) (apply-generic 'real-part z)) (define (imag-part z) (apply-generic 'imag-part z)) (define (magnitude z) (apply-generic 'magnitude z)) (define (angle z) (apply-generic 'angle z)) (define (make-from-real-imag x y) ((get 'make-from-real-imag 'rectangular) x y)) (define (make-from-mag-ang r a) ((get 'make-from-mag-ang 'polar) r a)) ;;; ;;; Generic function definition ;;; (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 (equ? x y) (apply-generic 'equ? x y)) (define (=zero? x) (apply-generic '=zero? x)) (define (square-root x) (apply-generic 'square-root x)) (define (arctangent x) (apply-generic 'arctangent x)) (define (cosine x) (apply-generic 'sine x)) (define (sine x) (apply-generic 'cosine x)) ;;; ;;; Scheme number package ;;; ; we turn this into two packages, since most of the code is identical, we will ; turn the old function into a template, and then use it with some addendums (define (install-scheme-number-package-template subtype-tag) (define (tag x) (attach-tag subtype-tag x)) (put 'add (list subtype-tag subtype-tag) (lambda (x y) (tag (+ x y)))) (put 'sub (list subtype-tag subtype-tag) (lambda (x y) (tag (- x y)))) (put 'mul (list subtype-tag subtype-tag) (lambda (x y) (tag (* x y)))) (put 'div (list subtype-tag subtype-tag) (lambda (x y) (tag (/ x y)))) ; part of exercise 2.79 (put 'equ? (list subtype-tag subtype-tag) =) ; part of exercise 2.80 (put '=zero? (list subtype-tag) (lambda (x) (zero? x))) (put 'sine subtype-tag (lambda (x) (tag (sin x)))) (put 'cosine subtype-tag (lambda (x) (tag (sin x)))) (put 'arctangent subtype-tag (lambda (x) (tag (atan x)))) (put 'arctangent (list subtype-tag subtype-tag) (lambda (x y) (tag (atan x y)))) (put 'square-root subtype-tag (lambda (x) (tag (sqrt x)))) 'done) (define (install-integer-package) (install-scheme-number-package-template 'integer) (put 'make 'integer (lambda (x) (attach-tag 'integer (inexact->exact (round x)))))) ; if a user gives a float or fraction to make-integer, we will simply round it (define (install-real-package) (install-scheme-number-package-template 'real) (put 'make 'real (lambda (x) (attach-tag 'real (exact->inexact x))))) ; if a user gives a scheme rational or integer to make a real, we will convert ; it (define (tag-scheme-number x) (cond ((integer? x) (cons 'integer x)) ((real? x) (cons 'real x)) (else x))) ; sometimes we want (install-integer-package) (install-real-package) (define (make-integer n) ((get 'make 'integer) n)) (define (make-real n) ((get 'make 'real) n)) ;;; ;;; Rational number package ;;; (define (install-rational-package) ;; internal procedures (define (numer x) (car x)) (define (denom x) (cdr x)) ; Modified to make exercise 2.79 easier to implement (define (make-rat n d) (let ((g (gcd n d)) (sign-flip (if (< d 0) -1 1))) (cons (/ n g sign-flip) (/ d g sign-flip)))) (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)))) ;; 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 'make 'rational (lambda (n d) (tag (make-rat n d)))) (put 'numer 'rational numer) (put 'denom 'rational denom) ; part of exercise 2.79 (put 'equ? '(rational rational) (lambda (x y) (and (= (numer x) (numer y)) (= (denom x) (denom y))))) ; part of exercise 2.80 (put '=zero? '(rational) (lambda (x) (= (numer x) 0))) (put 'sine '(rational) (lambda (x) (tag (sin (/ (numer x) (denom x)))))) (put 'cosine '(rational) (lambda (x) (tag (sin (/ (numer x) (denom x)))))) (put 'arctangent '(rational) (lambda (x) (tag (atan (/ (numer x) (denom x)))))) (put 'arctangent '(rational rational) (lambda (x y) (tag (atan (/ (numer x) (denom x)) (/ (numer y) (denom y)))))) (put 'square-root '(rational) (lambda (x) (tag (sqrt (/ (numer x) (denom x)))))) 'done) (install-rational-package) (define (make-rational n d) ((get 'make 'rational) n d)) (define (numer x) (apply-generic 'numer x)) (define (denom x) (apply-generic 'denom x)) ;;; ;;; Complex number package ;;; (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)) (define (make-from-mag-ang r a) ((get 'make-from-mag-ang 'polar) r a)) ;; internal procedures (define (add-complex z1 z2) (make-from-real-imag (add (real-part z1) (real-part z2)) (add (imag-part z1) (imag-part z2)))) (define (sub-complex z1 z2) (make-from-real-imag (sub (real-part z1) (real-part z2)) (sub (imag-part z1) (imag-part z2)))) (define (mul-complex z1 z2) (make-from-mag-ang (mul (magnitude z1) (magnitude z2)) (add (angle z1) (angle z2)))) (define (div-complex z1 z2) (make-from-mag-ang (div (magnitude z1) (magnitude z2)) (sub (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 'make-from-mag-ang 'complex (lambda (r a) (tag (make-from-mag-ang r a)))) ; Code added as fix in the exercise 2.77 (put 'real-part '(complex) real-part) (put 'imag-part '(complex) imag-part) (put 'magnitude '(complex) magnitude) (put 'angle '(complex) angle) ; Code added as fix in the exercise ; part of exercise 2.79 (put 'equ? '(complex complex) (lambda (z1 z2) (and (equ? (real-part z1) (real-part z2)) (equ? (imag-part z1) (imag-part z2))))) ; part of exercise 2.80 (put '=zero? '(complex) (lambda (z) (equ? (magnitude z) (make-integer 0)))) 'done) (install-complex-package) (define (make-complex-from-real-imag x y) ((get 'make-from-real-imag 'complex) x y)) (define (make-complex-from-mag-ang r a) ((get 'make-from-mag-ang 'complex) r a)) ;;; ;;; Raise package ;;; (define (install-raise-package) (define (integer->rational n) (make-rational n 1)) (define (rational->real x) (define tagged-x (attach-tag 'rational x)) (make-real (exact->inexact (/ (numer tagged-x) (denom tagged-x))))) (define (real->complex x) (make-complex-from-real-imag x 0)) (put 'raise 'integer integer->rational) (put 'raise 'rational rational->real) (put 'raise 'real real->complex) ; added in exercise 2.85 ; removed in exercise 2.86, because this projection is now better ; covered by real-part ;;;(define (complex->real z) ;;; (make-real (real-part z))) ; we will drop the real directly to integer, not to rational ; we could use inexact->exact to convert it to a rational, but this would be ; worse, since rationals are made up of bigints, there will always be a ; fraction close enough to be equ?. We will only simplify when the real is ; actually an integer. ; In principle, we could drop it to rational and check if the numerator and ; deominator are "small enough", but ; 1) this is beyond the scope of the exercise ; 2) even numbers as simple as 0.333..., would fail to be simplified this way (define (real->integer x) (make-integer x)) ; we get to use the builtin rounding we added, ha! (define (rational->integer x) (define tagged-x (attach-tag 'rational x)) (make-integer (/ (numer tagged-x) (denom tagged-x)))) (put 'project 'complex real-part) (put 'project 'real real->integer) (put 'project 'rational rational->integer)) (install-raise-package) (define (raise x) (apply-generic 'raise x)) (define (drop x) (define (re-raise-until obj original-type) (if (eq? (type-tag obj) original-type) obj (re-raise-until (raise obj) original-type))) (let ((tag (type-tag x))) (if (eq? tag 'integer) x (let ((dropped-x (apply-generic 'project x))) (let ((re-raised-x (re-raise-until dropped-x tag))) (if (equ? re-raised-x x) (drop dropped-x) x)))))) (define (subtype t1 t2) (let* ((type-hier '(integer rational real complex)) (t1-supertypes (cdr (memq t1 type-hier)))) (not (eq? #f (memq t2 t1-supertypes))))) (define (supertype t1 t2) (subtype t2 t1)) ; If we want to be able to wrap complex numbers around ordinary or rational ; numbers, we must be able to do the following: ; 1) As mentioned, we need sine/cosine/atan functions that work on other types of numbers. ; We will install it in the integer, rational and real package. ; 2) We need the rectangular/polar packages to explicitly handle nesting of these other ; number types while selecting, constructing or just operating on complex numbers ; 3) These packages must, at the same time, prohibit the use of complex numbers ; as arguments here: for example: ; (make-from-real-imag (make-rational 1 4) 5) should produce something like: ; '(complex rectangular (rational 1 . 4) . (integer . 5)), same as ; '(complex rectangular (rational 1 . 4) integer . 5) ; a call for: ; (make-from-real-imag (make-from-real-imag 3 1) ; 5) ; should produce an error