【SICP练习】24 练习1.30
来源:互联网 发布:windows 10关闭快捷键 编辑:程序博客网 时间:2024/06/06 01:39
练习1.30
这道题要求将一个递归的规程改成是迭代的,通过对递归版本的sum的观察得出以下变换形式:
a—(next a)
other—(+ (term a) other)
博主英文不太好实在想不出什么好词语了。变换成迭代通俗点讲就是要将sum中最后一行的加号去掉,因为这个是消耗大量空间的根源。
如果我们将a和other作为迭代中的产生,将这个过程展开即有:
(sum-iter a other)
(sum-iter (next a) (+ (term a) other))
(sum-iter (next (next a)) (+ (term (term a))(+ (term a) other)))
博主就不再一直写下去了,就是按照前面的变换规则做对应变换罢了。
于是我们便可以完成题中的空缺部分了。
(define (sum term a next b)
(define (sum-iter a other)
(if (> a b)
other
(sum-iter (next a) (+ (term a) other))))
(sum-iter a 0))
5 0
- 【SICP练习】24 练习1.30
- 【SICP练习】2 练习1.6
- 【SICP练习】3 练习1.7
- 【SICP练习】4 练习1.8
- 【SICP练习】5 练习1.9
- 【SICP练习】6 练习1.10
- 【SICP练习】7 练习1.11
- 【SICP练习】8 练习1.12
- 【SICP练习】9 练习1.15
- 【SICP练习】10 练习1.16
- 【SICP练习】11 练习1.17
- 【SICP练习】12 练习1.18
- 【SICP练习】13 练习1.19
- 【SICP练习】14 练习1.20
- 【SICP练习】15 练习1.21
- 【SICP练习】16 练习1.22
- 【SICP练习】17 练习1.23
- 【SICP练习】18 练习1.24
- Git使用手册:创建一个Git仓库
- Action接收参数的3种方式(属性,域模型,模型驱动,struts2.1.8)
- c++ primer
- Android中的windowSoftInputMode属性详解
- 去百度API的百度地图准确叠加和坐标转换的解决方案研究
- 【SICP练习】24 练习1.30
- java 获得用户访问的真实IP地址
- const_cast使用
- QML入门教程
- cocos2dx- call to OpenGL ES API with no current context(logged once per thread)
- DSAPI多功能组件编程应用-DS密法
- 【SICP练习】25 练习1.31
- 拾起过去
- Andrew Stankevich's Contest 23