这次回顾第二章第二部分习题。
学习资料:
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.17(p69)
1 | (define (last-pair arr) |
结果如下:
1 | 34 |
2.18(p69)
1 | (load "helper.scm") |
结果如下:
1 | (25 16 9 4 1) |
2.19(p69)
1 | (define (no-more? arr) |
结果如下:
1 | 292 |
2.20(p69)
1 | (define (judge a b) |
结果如下:
1 | (1 3 5 7) |
2.21(p71)
1 | (load "helper.scm") |
结果如下:
1 | (1 4 9 16) |
2.22(p72)
1 | (load "helper.scm") |
结果测试:
1 | (16 9 4 1) |
方法1的迭代过程如下:
1 | things answer |
方法2 cons的一个元素是列表,但我们希望的是一个数。
2.23(p72)
参考资料:
https://sicp.readthedocs.io/en/latest/chp2/23.html
1 | (define (for-each proc arr) |
结果如下:
1 | 57 |
2.24(p73)
1 | > (list 1 (list 2 (list 3 4))) |
2.25(p74)
1 | (define a (list 1 3 (list 5 7) 9)) |
结果如下:
1 | 7 |
2.26(p74)
1 | (load "helper.scm") |
结果如下:
1 | (1 2 3 4 5 6) |
2.27(p74)
参考资料:
https://sicp.readthedocs.io/en/latest/chp2/27.html
几种方法的比较:
1 | (load "helper.scm") |
结果如下:
1 | ((1 2) (3 4) (5 6)) |
2.28(p74)
参考资料:
https://sicp.readthedocs.io/en/latest/chp2/28.html
1 | (load "helper.scm") |
结果如下:
1 | (1 2 3 4) |
2.29(p74)
参考资料:
https://sicp.readthedocs.io/en/latest/chp2/29.html
参考了网上的资料,核心是利用递归:
1 | ; (load "2.29_1.scm") |
2.29_1.scm:
1 | (define (make-mobile left right) |
2.29_2.scm:
1 | (define (make-mobile left right) |
结果如下:
1 | 9 |
2.30(p75)
1 | (load "helper.scm") |
结果如下:
1 | (1 (4 (9 16) 25) (36 49)) |
2.31(p76)
1 | (load "helper.scm") |
结果如下:
1 | (1 (4 (9 16) 25) (36 49)) |
2.32(p76)
思路是对rest中每个元素(集合)添加(car s)
1 | (load "helper.scm") |
结果如下:
1 | (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)) |
值的回顾的习题
2.23, 2.26, 2.27, 2.29, 2.31