SICP 习题 1.18是习题1.17的扩展,要求我们将1.17题中的对数步数求加法的过程修改成迭代方式。同时,习题1.18习题也是对习题1.16的强调,习题1.16是通过迭代方法求幂,这里是通过迭代方法求加法。
所以我们需要回过头去看习题1.16和1.17。
习题1.16是将以下函数进行修改:
(define (fast-expt b n)
(cond ((= n 0) 1)
((even? n) (square (fast-expt b (/ n 2))))
(else (* b (fast-expt b (- n 1))))))
变成了这样:
(define (fast-expt-iter root cur-result n)
(cond ((= n 0) 1)
((= n 1) (* root cur-result))
((even? n) (fast-expt-iter (square root) cur-result (/ n 2)))
(else (fast-expt-iter root (* root cur-result) (- n 1)))))
而习题1.17的结果如下:
(define (fast-mul a b)
(cond ((= b 0) 0)
((even? b) (double (fast-mul a (halve b ))))
(else (+ a (fast-mul a (- b 1))))))
我们发现习题1.16修改前的代码和习题1.17的结果几乎是一样的。
所以,我们可以完全按照相同的思路完成迭代方法求加法的代码:
(define (fast-mul-iter a cur-result b)
(cond ((= b 0) 0)
((= b 1) (+ a cur-result))
((even? b) (fast-mul-iter (double a) cur-result (halve b)))
(else (fast-mul-iter a (+ a cur-result) (- b 1)))))
所以说习题1.18没有什么难度,前提是你能够独立完成习题1.16和习题1.17 。
分享到:
相关推荐
SICP 习题答案 计算机程序的构造和解释 1-3章 习题答案
SICP习题解答,主要第一章的内容习题答案
NULL 博文链接:https://pengpeng.iteye.com/blog/1344689
SICP 解题集
sicp in python 中文版 sicp in python 中文版 sicp in python 中文版 !!!download>>>https://github.com/wizardforcel/sicp-py-zh
SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版
SICP-Python版本
SICP 使用的scheme解释器 以前叫DrScheme
sicp 2.2.4节图形语言的racket程序包,配置路径,C:\Users\Administrator\AppData\Roaming\Racket
Python SICP epub版本,很适合学习抽象的思想,用Python版本比lisp更实用
SICP CHINESE ENGLISH THE SECOND EDITION SICP CHINESE ENGLISH THE SECOND EDITION
sicp in python 中文版 sicp in python 中文版 sicp in python 中文版 download : https://github.com/wizardforcel/sicp-py-zh
资源来自pypi官网。 资源全名:sicp-0.0.1b102.dev4.tar.gz
sicp 2ed高清pdf,以及相对应的mit课程资料及习题答案打包,中文版的视频在这里http://i.youku.com/i/UNTcxODk3ODQw/videos?spm=a2hzp.8244740.0.0
SICP 解题集《计算机程序的构造和解释》练习题解集。安装 MIT/GNU Scheme(macOS)下载:下载后运行 .dmg 文件,把 MIT/GNU Scheme.app 拖入 Applications 文件夹在 Applications/应用程序 文件夹中找到 MIT/GNU ...
sicp-in-python(中文版+英文版)PDF 背景. SICP 全称Structure and Interpretation of Computer Programs,翻译过来叫《计算机程序的构造和解释》使用python
资源名称:sicp 和 操作系统:精髓与设计原理第七版资源截图: 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。
经典书籍《计算机程序的构造与解释》,UCB热门课程CS61a的官方教材
#SICP SICP解决方案