【SICP练习】26 练习1.32
2015-09-08 00:00
337 查看
练习1.32
因为递归比迭代要更容易实现,因此我先考虑的递归。先将sum和product都列出来。
(define (sum term a next b)
(if(> a b)
0
(+(term a)
(sum term (next a) next b))))
(define (product term a next b)
(if(> a b)
1
(* (term a)
(product term (next a) next b))))
通过对比我们发现,仅仅是有2个地方的区别。按照题中的要去,我们将0或1的位置用null-value代替,将+或*用combiner代替。在函数的参数中添加这两个新的参数即可。通过对比,其实也不难嘛。
(define (accumulate combinernull-value term a next b)
(if (> a b)
null-value
(combiner (term a) (accumulate combinernull-value term (next a) next b))))
题中还要求我们定义出sum和product来,这里我就列出sum的递归accumulate版本。
(define (sum term a next b)
(accumulate + 0 term a next b))
接下来我们再看看如何写出迭代版本的accumulate。还是一样,先列出迭代版本的sum和product。
(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))
(define (product term a next b)
(define (product-iter a other)
(if (> a b)
other
(product-iter (next a)
(* (term a) other))))
(product-iter a 1))
同样是通过类比,我们又可以写出迭代版本的accumulate。
(define (accumulate combinernull-value term a next b)
(define (accumulate-iter a other)
(if (> a b)
other
(accumulate-iter (next a)
(combiner (term a)other))))
(accumulate-iter a null-value))
这次我们就来写迭代版本的product。
(define (product term a nextb)
(accumulate * 1 term a next b))
通过这些对比,感觉枯燥的递归和迭代还挺有意思的。
版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http://blog.csdn.net/nomasp
练习1.32
因为递归比迭代要更容易实现,因此我先考虑的递归。先将sum和product都列出来。
(define (sum term a next b)
(if(> a b)
0
(+(term a)
(sum term (next a) next b))))
(define (product term a next b)
(if(> a b)
1
(* (term a)
(product term (next a) next b))))
通过对比我们发现,仅仅是有2个地方的区别。按照题中的要去,我们将0或1的位置用null-value代替,将+或*用combiner代替。在函数的参数中添加这两个新的参数即可。通过对比,其实也不难嘛。
(define (accumulate combinernull-value term a next b)
(if (> a b)
null-value
(combiner (term a) (accumulate combinernull-value term (next a) next b))))
题中还要求我们定义出sum和product来,这里我就列出sum的递归accumulate版本。
(define (sum term a next b)
(accumulate + 0 term a next b))
接下来我们再看看如何写出迭代版本的accumulate。还是一样,先列出迭代版本的sum和product。
(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))
(define (product term a next b)
(define (product-iter a other)
(if (> a b)
other
(product-iter (next a)
(* (term a) other))))
(product-iter a 1))
同样是通过类比,我们又可以写出迭代版本的accumulate。
(define (accumulate combinernull-value term a next b)
(define (accumulate-iter a other)
(if (> a b)
other
(accumulate-iter (next a)
(combiner (term a)other))))
(accumulate-iter a null-value))
这次我们就来写迭代版本的product。
(define (product term a nextb)
(accumulate * 1 term a next b))
通过这些对比,感觉枯燥的递归和迭代还挺有意思的。
版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http://blog.csdn.net/nomasp
相关文章推荐
- C#中的递归APS和CPS模式详解
- WinForm实现按名称递归查找控件的方法
- C#中的尾递归与Continuation详解
- C#递归实现显示文件夹及所有文件并计算其大小的方法
- php递归创建目录的方法
- 浅谈javascript 迭代方法
- Javascript递归打印Document层次关系实例分析
- oracle 使用递归的性能提示测试对比
- 使用curl递归下载软件脚本分享
- Perl脚本实现递归遍历目录下的文件
- JavaScript的递归之递归与循环示例介绍
- C# 递归查找树状目录实现方法
- 全排列算法的非递归实现与递归实现的方法(C++)
- php递归列出所有文件和目录的代码
- java递归菜单树转换成pojo对象
- 一个JavaScript递归实现反转数组字符串的实例
- Java中的递归详解(用递归实现99乘法表来讲解)
- C语言的递归思想实例分析
- php通过递归方式复制目录和子目录的方法
- php递归法读取目录及文件的方法