这次回顾第二章第三部分习题。
学习资料:
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.33(p80)
1 | (define (map p sequence) |
结果如下:
1 | (0 4 16 36 64) |
2.34(p80)
1 | (load "helper.scm") |
结果如下:
1 | 79 |
2.35(p81)
参考资料:
https://sicp.readthedocs.io/en/latest/chp2/35.html
1 | (load "helper.scm") |
结果如下:
1 | 4 |
2.36(p81)
2.36_helper.scm:
1 | (define (first-element seqs) |
测试函数:
1 | (load "helper.scm") |
结果如下:
1 | (22 26 30) |
2.37(p81)
1 | (load "2.36_helper.scm") |
结果如下:
1 | 10 |
2.38(p82)
1 | (load "helper.scm") |
结果如下:
1 | 3/2 |
2.39(p82)
参考资料:
https://sicp.readthedocs.io/en/latest/chp2/39.html
1 | (load "helper.scm") |
结果如下:
1 | (25 16 9 4 1) |
2.40(p84)
1 | (load "helper.scm") |
结果如下:
1 | ((2 1 3) (3 2 5) (4 1 5) (4 3 7) (5 2 7) (6 1 7) (6 5 11) (7 4 11) (7 6 13)) |
2.41(p84)
这里限制
代码如下:
1 | (load "helper.scm") |
结果如下:
1 | ((13 11 10) (13 12 9) (14 11 9) (14 12 8) (14 13 7) (15 10 9) (15 11 8) (15 12 7) (15 13 6) (15 14 5) (16 10 8) (16 11 7) (16 12 6) (16 13 5) (16 14 4) (16 15 3) (17 9 8) (17 10 7) (17 11 6) (17 12 5) (17 13 4) (17 14 3) (17 15 2) (17 16 1) (18 9 7) (18 10 6) (18 11 5) (18 12 4) (18 13 3) (18 14 2) (18 15 1) (19 8 7) (19 9 6) (19 10 5) (19 11 4) (19 12 3) (19 13 2) (19 14 1) (20 8 6) (20 9 5) (20 10 4) (20 11 3) (20 12 2) (20 13 1) (21 7 6) (21 8 5) (21 9 4) (21 10 3) (21 11 2) (21 12 1) (22 7 5) (22 8 4) (22 9 3) (22 10 2) (22 11 1) (23 6 5) (23 7 |
2.42(p84)
8-queens.scm:
1 | (define empty-board '()) |
测试代码:
1 | (load "helper.scm") |
结果如下:
1 | 1 |
2.43(p85)
1 | (load "helper.scm") |
结果如下:
1 | ((3 1 4 2) (2 4 1 3)) |
该程序慢,是因为对于
1 | (enumerate-interval 1 board-size) |
中每个元素,都调用了
1 | (queen-cols (- k 1)) |
所以总的时间复杂度为