计算机程序的构造和解释(SICP) 第2章 习题解析 Part6
这次回顾第二章第六部分习题。
学习资料:
https://github.com/DeathKing/Learning-SICP
https://mitpress.mit.edu/sites/default/files/sicp/index.html
https://www.bilibili.com/video/BV1Xx41117tr?from=search&seid=14983483066585274454
参考资料:
https://sicp.readthedocs.io/en/latest
2.73(p125)
(a)
- 判断是否是数字。
- 判断exp是否和var相同。
- 根据(operator exp)判断deriv的类型,然后调用对应的求导函数。
number?, variable?, same-variable?都无法调用(operator exp),所以无法加入数据导向分派中。
(b), (c)
(load "table_helper.scm")
(load "helper.scm")
(load "2.73_helper.scm")
; basic deriv
(define (deriv exp var)
(cond ((number? exp) 0)
((variable? exp) (if (same-variable? exp var) 1 0))
(else ((get 'deriv (operator exp)) (operands exp)
var))))
(define (operator exp) (car exp))
(define (operands exp) (cdr exp))
; (b)
(define (install-sum-package)
(define (derive-sum exp var)
(make-sum (deriv (addend exp) var)
(deriv (augend exp) var)))
(put 'deriv '+ derive-sum)
'done)
(define (install-product-package)
(define (derive-product exp var)
(make-sum
(make-product (multiplier exp)
(deriv (multiplicand exp) var))
(make-product (deriv (multiplier exp) var)
(multiplicand exp))))
(put 'deriv '* derive-product)
'done)
; (c)
(define (install-exponentiation-package)
(define (derive-exponentiation exp var)
(let ((b (base exp))
(e (exponent exp)))
(make-product
(make-product e (make-exponentiation b (- e 1)))
(deriv b var))))
(put 'deriv '** derive-exponentiation)
'done)
(install-exponentiation-package)
(install-sum-package)
(install-product-package)
; test
(define a '(+ x 3))
(define b '(* x 5))
(define c '(** x 5))
(newline)
(display (deriv a 'x))
(newline)
(display (deriv b 'x))
(newline)
(display (deriv c 'x))
(exit)
结果如下:
1
5
(* 5 (** x 4))
(d)
修改put部分即可,代码如下:
(load "table_helper.scm")
(load "helper.scm")
(load "2.73_helper.scm")
; basic deriv
(define (deriv exp var)
(cond ((number? exp) 0)
((variable? exp) (if (same-variable? exp var) 1 0))
(else ((get (operator exp) 'deriv) (operands exp)
var))))
(define (operator exp) (car exp))
(define (operands exp) (cdr exp))
; (b)
(define (install-sum-package)
(define (derive-sum exp var)
(make-sum (deriv (addend exp) var)
(deriv (augend exp) var)))
(put '+ 'deriv derive-sum)
'done)
(define (install-product-package)
(define (derive-product exp var)
(make-sum
(make-product (multiplier exp)
(deriv (multiplicand exp) var))
(make-product (deriv (multiplier exp) var)
(multiplicand exp))))
(put '* 'deriv derive-product)
'done)
; (c)
(define (install-exponentiation-package)
(define (derive-exponentiation exp var)
(let ((b (base exp))
(e (exponent exp)))
(make-product
(make-product e (make-exponentiation b (- e 1)))
(deriv b var))))
(put '** 'deriv derive-exponentiation)
'done)
(install-exponentiation-package)
(install-sum-package)
(install-product-package)
;test
(define a '(+ x 3))
(define b '(* x 5))
(define c '(** x 5))
(newline)
(display (deriv a 'x))
(newline)
(display (deriv b 'x))
(newline)
(display (deriv c 'x))
(exit)
结果如下:
1
5
(* 5 (** x 4))
2.74(p126)
分为两层结构。
第一层:
op type
getRecord companyName -> set
第二层:
op type
getData address
getData salary
具体实现从略。
2.75(p128)
(define (make-from-mag-ang r a)
(define (dispatch op)
(cond ((eq? op 'magnitude) r)
((eq? op 'angle) a)
((eq? op 'real-part) (* r (cos a)))
((eq? op 'imag-part) (* r (sin a)))
(else
(error "Unknown op -- MAKE-FROM-REAL-IMAG" op))))
dispatch)
; test
(define a (make-from-mag-ang 1 0))
(newline)
(display (a 'magnitude))
(newline)
(display (a 'angle))
(newline)
(display (a 'real-part))
(newline)
(display (a 'imag-part))
(exit)
结果如下:
1
0
1
0
2.76(p128)
- 显示分派:最糟糕。
- 数据导向(按行):适合加入新操作。
- 消息传递(按列):适合增加新类型。
2.77(p132)
参考资料:
https://sicp.readthedocs.io/en/latest/chp2/77.html
报错原因是因为不存在type为complex,op为magnitude的函数。
整体结构
- arithmetic.scm
- tag.scm
- apply.scm
- table_helper.scm
- complex.scm
- scheme_ari.scm
- rational_ari.scm
- complex_ari.scm
运行代码如下:
(load "arithmetic.scm")
(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 (magnitude z) (apply-generic 'magnitude z))
(define z1 (make-complex-from-real-imag 1 2))
(define z2 (make-complex-from-real-imag 2 2))
(define z3 (add z1 z2))
(newline)
(display z3)
(newline)
(display (magnitude z3))
(exit)
结果如下:
(complex rectangular 3 . 4)
5
每个部分分别如下:
arithmetic.scm(自己编写):
(load "tag.scm")
(load "apply.scm")
(load "table_helper.scm")
(load "helper.scm")
(load "complex.scm")
(load "scheme_ari.scm")
(load "rational_ari.scm")
(load "complex_ari.scm")
(install-rectangular-package)
(install-polar-package)
(install-scheme-number-package)
(install-rational-package)
(install-complex-package)
tag.scm:
(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)))
apply.scm:
(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))
(error
"No method for these types -- APPLY-GENERIC"
(list op type-tags))))))
该函数对不同类型变量调用通用的方法。
table_helper.scm:
(define false #f)
(define true #t)
;; local tables
(define (make-table)
(let ((local-table (list '*table*)))
(define (lookup key-1 key-2)
(let ((subtable (assoc key-1 (cdr local-table))))
(if subtable
(let ((record (assoc key-2 (cdr subtable))))
(if record
(cdr record)
false))
false)))
(define (insert! key-1 key-2 value)
(let ((subtable (assoc key-1 (cdr local-table))))
(if subtable
(let ((record (assoc key-2 (cdr subtable))))
(if record
(set-cdr! record value)
(set-cdr! subtable
(cons (cons key-2 value)
(cdr subtable)))))
(set-cdr! local-table
(cons (list key-1
(cons key-2 value))
(cdr local-table)))))
'ok)
(define (dispatch m)
(cond ((eq? m 'lookup-proc) lookup)
((eq? m 'insert-proc!) insert!)
(else (error "Unknown operation -- TABLE" m))))
dispatch))
(define operation-table (make-table))
(define get (operation-table 'lookup-proc))
(define put (operation-table 'insert-proc!))
complex.scm:
;; complex number
(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))))
(put 'equ? '(rectangular rectangular)
(lambda (z1 z2) (and (= (real-part z1) (real-part z2))
(= (imag-part z1) (imag-part z2)))))
(put 'zero? '(rectangular)
(lambda (z) (and (= (real-part z) 0)
(= (imag-part z) 0))))
'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))))
;新增
(put 'equ? '(polar polar)
(lambda (z1 z2) (and (= (real-part z1) (real-part z2))
(= (imag-part z1) (imag-part z2)))))
(put 'zero? '(polar)
(lambda (z) (= (magnitude z) 0)))
'done)
scheme_ari.scm(新增apply-generic部分以及put部分):
;; arithmetic operations
(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 'equ? '(scheme-number scheme-number)
(lambda (x y) (= x y)))
(put 'zero? '(scheme-number)
(lambda (x) (= x 0)))
'done)
(define (make-scheme-number n)
((get 'make 'scheme-number) n))
rational_ari.scm:
(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))))
;; 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 'equ? '(rational rational)
(lambda (x y) (= (* (numer x) (denom y))
(* (numer y) (denom x)))))
(put 'zero? '(rational)
(lambda (x) (= (numer x) 0)))
'done)
(define (make-rational n d)
((get 'make 'rational) n d))
complex_ari.scm:
;新增
(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 (equ? z1 z2) (apply-generic 'equ? z1 z2))
(define (zero? z) (apply-generic 'zero? 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))
(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))))
;新增
(put 'real-part '(complex) real-part)
(put 'imag-part '(complex) imag-part)
(put 'magnitude '(complex) magnitude)
(put 'angle '(complex) angle)
;新增
(put 'equ? '(complex complex) equ?)
(put 'zero? '(complex) zero?)
'done)
(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))
2.78(p132)
(load "tag_v1.scm")
(load "apply.scm")
(load "table_helper.scm")
(load "helper.scm")
(load "complex.scm")
(load "scheme_ari_v1.scm")
(load "rational_ari.scm")
(load "complex_ari.scm")
(install-rectangular-package)
(install-polar-package)
(install-scheme-number-package)
(install-rational-package)
(install-complex-package)
(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 (magnitude z) (apply-generic 'magnitude z))
(define (test x y)
(display "x: ")
(display x)
(newline)
(display "y: ")
(display y)
(newline)
(display (add x y))
(newline)
(display (sub x y))
(newline)
(display (mul x y))
(newline)
(display (div x y))
(newline))
(define a1 (make-scheme-number 1))
(define a2 (make-scheme-number 2))
(define b1 (make-rational 1 3))
(define b2 (make-rational 2 3))
(define z1 (make-complex-from-real-imag 1 2))
(define z2 (make-complex-from-real-imag 2 2))
(test a1 a2)
(test b1 b2)
(test z1 z2)
(exit)
结果如下:
x: 1
y: 2
3
-1
2
1/2
x: (rational 1 . 3)
y: (rational 2 . 3)
(rational 1 . 1)
(rational 1 . -3)
(rational 2 . 9)
(rational 1 . 2)
x: (complex rectangular 1 . 2)
y: (complex rectangular 2 . 2)
(complex rectangular 3 . 4)
(complex rectangular -1 . 0)
(complex polar 6.324555320336759 . 1.8925468811915387)
(complex polar 0.7905694150420948 . 0.32175055439664213)
修改的部分:
tag_v1.scm:
(define (attach-tag type-tag contents)
(cons type-tag contents))
(define (type-tag datum)
(cond ((number? datum) 'scheme-number)
((pair? datum) (car datum))
(else (error "Bad tagged datum -- TYPE-TAG" datum))))
(define (contents datum)
(cond ((number? datum) datum)
((pair? datum) (cdr datum))
(else (error "Bad tagged datum -- CONTENTS" datum))))
scheme_ari_v1.scm:
;; arithmetic operations
(define (install-scheme-number-package)
(put 'add '(scheme-number scheme-number)
(lambda (x y) (+ x y)))
(put 'sub '(scheme-number scheme-number)
(lambda (x y) (- x y)))
(put 'mul '(scheme-number scheme-number)
(lambda (x y) (* x y)))
(put 'div '(scheme-number scheme-number)
(lambda (x y) (/ x y)))
(put 'make 'scheme-number
(lambda (x) x))
'done)
(define (make-scheme-number n)
((get 'make 'scheme-number) n))
以下两题新增的部分见2.77
2.79(p132)
(load "arithmetic.scm")
(define (equ? x y) (apply-generic 'equ? x y))
(define (test x y)
(display "x: ")
(display x)
(newline)
(display "y: ")
(display y)
(newline)
(display (equ? x y))
(newline))
(define a1 (make-scheme-number 1))
(define a2 (make-scheme-number 2))
(define b1 (make-rational 1 3))
(define b2 (make-rational 2 3))
(define z1 (make-complex-from-real-imag 1 2))
(define z2 (make-complex-from-real-imag 2 2))
; test
(test a1 a2)
(test a1 a1)
(test b1 b2)
(test b1 b1)
(test z1 z2)
(test z1 z1)
(exit)
结果如下:
x: (scheme-number . 1)
y: (scheme-number . 2)
#f
x: (scheme-number . 1)
y: (scheme-number . 1)
#t
x: (rational 1 . 3)
y: (rational 2 . 3)
#f
x: (rational 1 . 3)
y: (rational 1 . 3)
#t
x: (complex rectangular 1 . 2)
y: (complex rectangular 2 . 2)
#f
x: (complex rectangular 1 . 2)
y: (complex rectangular 1 . 2)
#t
2.80(p132)
(load "arithmetic.scm")
(define (zero? x) (apply-generic 'zero? x))
(define (test x)
(display "x: ")
(display x)
(newline)
(display (zero? x))
(newline))
(define a1 (make-scheme-number 1))
(define a2 (make-scheme-number 0))
(define b1 (make-rational 1 3))
(define b2 (make-rational 0 6))
(define z1 (make-complex-from-real-imag 1 2))
(define z2 (make-complex-from-real-imag 0 0))
(define z3 (make-complex-from-mag-ang 1 0))
(define z4 (make-complex-from-mag-ang 0 5))
; test
(test a1)
(test a2)
(test b1)
(test b2)
(test z1)
(test z2)
(test z3)
(test z4)
(exit)
结果如下:
x: (scheme-number . 1)
#f
x: (scheme-number . 0)
#t
x: (rational 1 . 3)
#f
x: (rational 0 . 1)
#t
x: (complex rectangular 1 . 2)
#f
x: (complex rectangular 0 . 0)
#t
x: (complex polar 1 . 0)
#f
x: (complex polar 0 . 5)
#t
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Doraemonzzz!
评论
ValineLivere