计算机程序的构造和解释(SICP) Assignment 3 Graphing with higher-order procedures
这次回顾Assignment 3 Graphing with higher-order procedures。
学习资料:
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://mitpress.mit.edu/sites/default/files/sicp/psets/index.html
1. Procedure Types and Procedure Constructors
Exercise 1.A
(define (thrice f)
(compose (compose f f) f))
((thrice thrice) f)
((compose (compose thrice thrice) thrice)) f)
((compose (lambda (x) (thrice (thrice x))) thrice)) f)
((lambda (x) ((lambda (x) (thrice (thrice x))) (thrice x))) f)
(repeated f 27)
代码对应的效果:
(((thrice thrice) 1+) 6)
6 + 27 = 33
(((thrice thrice) identity) compose)
identity是恒等映射,所以最后的结果为compose。
(((thrice thrice) square) a)
所以
(((thrice thrice) square) 1)
为$1$;
(((thrice thrice) square) 2)
为$2^{2^{27}}$
Exercise 1.B
代码验证:
(load "utils.scm")
(((thrice thrice) 1+) 6)
(((thrice thrice) identity) compose)
(((thrice thrice) square) 1)
; 数字太大导致溢出
; (((thrice thrice) square) 2)
实验结果:
1 ]=> (((thrice thrice) 1+) 6)
;Value: 33
1 ]=> (((thrice thrice) identity) compose)
;Value 13: #[compound-procedure 13 compose]
1 ]=> (((thrice thrice) square) 1)
;Value: 1
1 ]=> ; 数字太大导致溢出
; (((thrice thrice) square) 2)
2. Curves as Procedures and Data
Exercise 2
1
2
代码:
(load "curves.scm")
(define (vertical-line point length)
(lambda (t)
(make-point
(x-of point)
(+ (y-of point) (* t length)))))
3
Exercise 3
映射关系:
代码:
(load "curves.scm")
(define (reflect-through-y-axis curve)
(lambda (t)
(let ((ct (curve t)))
(make-point
(- (x-of ct))
(y-of ct)))))
Exercise 4
参考资料:
先平移至重合,然后合并:
要使得
重合,做如下变换即可;
代码:
(load "curves.scm")
(define (connect-ends curve1 curve2)
(connect-rigidly
curve1
((translate (- (x-of (curve1 1)) (x-of (curve1 0)))
(- (y-of (curve1 1)) (y-of (curve1 0)))) curve2)))
3. Drawing Curves
由于没有图片,这部分从略。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Doraemonzzz!
评论
ValineLivere