sicp-solutions/chapter-2/ex-2.83-84.scm

373 lines
12 KiB
Scheme

#lang sicp
; Building on top of exercise 2.82
; 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) (* 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 (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))))
;; 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) (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)))
;; 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 (modified for exercise 2.82)
;;;
(define (apply-generic op . args)
(let ((type-tags (map type-tag args)))
(let ((proc (get op type-tags)))
(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)))))))
(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))
;;;
;;; 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)))
'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
(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)))
'done)
(install-rational-package)
(define (make-rational n d)
((get 'make 'rational) n d))
(define (numer x)
((get 'numer 'rational) x))
(define (denom x)
((get 'denom 'rational) 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 (+ (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 '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 (= (real-part z1) (real-part z2))
(= (imag-part z1) (imag-part z2)))))
; part of exercise 2.80
(put '=zero? '(complex)
(lambda (z) (= (magnitude z) 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)
(make-real (exact->inexact (/ (numer x) (denom 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))
(install-raise-package)
(define (raise x) (apply-generic 'raise 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))