1.30

Exercise 1.30. The sum procedure above generates a linear recursion. The procedure can be rewritten so
that the sum is performed iteratively. Show how to do this by filling in the missing expressions in the
following definition:

1.30的题目就是把一个递归方法sum改写成迭代。原来的sum方法是:

(defun sum (term a next b)(if (> a b)0(+ (funcall term a)(sum term (funcall next a) next b))))

变成下面这种样子:

(defun sum (term a next b)(defun iter (a result)(if <??><??>(iter <??> <??>)))(iter <??> <??>))

当然了,你需要补齐那些问号。实现起来很简单,只需要使用result记录第n次迭代时,前面n-1次的和就可以了。

(defun sum-iter (term a next b)(defun iter (a result)(if (> a b)result(iter (funcall next a) (+ result (funcall term a)))))(iter a 0))

1.31

1.30里面实现了累加,这里需要实现连乘了。同时给出递归和非递归版本。参考前面sum的实现,别忘了把加号改成乘号,然后把默认值从0变成1:

(defun product (term a next b)(if (> a b)1(* (funcall term a)(product term (funcall next a) next b))))(defun product-iter (term a next b)(defun iter (a result)(if (> a b)result(iter (funcall next a ) (* result (funcall term a)))))(iter a 1))
(product-iter #'(lambda (x) x) 1 #'(lambda (x) (+ x 1)) 3)

对于pi的运算,采用下面的公式:


 使用上面的共识,我们让a=1,b等于迭代的次数,下面需要确认的就是term方法了。

我们把上面一个公式,两个两个拆开,就变成了这样:

写道
2 × 4 / (3 * 3)
4 × 6 / (5 * 5)
6 × 8 / (7 * 7)
....

对于第n行,分母是 (2n+1)的平方, 分子是 2n × 2(n+1), 所以term应该是:

(defun pi-term (x)(/ (* (* 2 x) (* 2 (+ 1 x)))(* (+ 1 (* 2 x))(+ 1 (* 2 x)))))(* 4 (product #'pi-term 1 #'(lambda (x) (+ x 1)) 3))
(* 4 (product-iter #'pi-term 1 #'(lambda (x) (+ x 1)) 3))

1.32

把前面的sum、product再做一层抽象,编写一个accumulate函数,它与sum、product相比,多了两个参数:

combiner 对元素使用什么样的操作

null-value 元素使用完毕的值,所以,实现这样一个方法:(accumulate combiner null-value term a next b)

(defun accumulate (combiner null-value term a next b)(if (> a b)null-value(funcall combiner(funcall term a)(accumulate combiner null-value term (funcall next a) next b))))
(defun accumulate-iter (combiner null-value term a next b)(defun iter (a result)(if (> a b)(print result)(iter (funcall next a)(funcall combineraresult))))(iter a null-value))

下面轻松定义sum和product

(defun acc-sum (term a next b)(accumulate #'+ 0 term a next b))(defun acc-sum-iter (term a next b)(accumulate-iter #'+ 0 term a next b))(defun acc-product (term a next b)(accumulate #'* 1 term a next b))(defun acc-product-iter (term a next b)(accumulate-iter #'* 1 term a next b))

来测试一下:

(defun next (x) (+ x 1))
(acc-sum #'(lambda (x) x) 1 #'next 10)
(acc-sum-iter #'(lambda (x) x) 1 #'next 10)
(acc-product #'(lambda (x) x) 1 #'next 4)
(acc-product-iter #'(lambda (x) x) 1 #'next 4)

Exercise 1.30/1.31/1.32/1.33相关推荐

  1. 蔡高厅老师 - 高等数学阅读笔记 - 07 - 函数的微分 - 微分中值定理 罗尔、拉格朗日中值定理 (31、32、33、34、35)

    函数的微分: 记为: 等价无穷小, 微分的概念: 就是要寻求近似值,不要复杂 微分的几何意义 微分公式 33 微分中值定理: 四大定理 拉格朗日中值定理的应用: 数值不等式 首先,化为函数不等式 二阶 ...

  2. 11.28 限定某个目录禁止解析php 11.29 限制user_agent 11.30/11.31 php相关配置

    - 11.28 限定某个目录禁止解析php - 11.29 限制user_agent - 11.30/11.31 php相关配置 - 扩展 - apache开启压缩 http://ask.apelea ...

  3. 某美团程序员爆料:美团虽然屏蔽职级,但可以通过椅子判断!坐人体工学椅的至少是3-1和3-2的大佬!真是这样吗?...

    不同级别的程序员除了薪资待遇不一样,还有什么其他的差别吗?有人说:连配备的电脑.桌椅都不一样! 美团的专业职级体系从P1-1逐级提高,到P5为最高.一个新入职的美团程序员发帖爆料:mentor告诉他, ...

  4. 10.32/10.33 rsync通过服务同步 10.34 linux系统日志 screen工具

    通过后台服务的方式 在远程主机上建立一个rsync的服务器,在服务器上配置好rsync的各种应用,然后将本机作为rsync的一个客户端连接远程的rsync服务器. 在128主机上建立并配置rsync的 ...

  5. 10.32/10.33 rsync通过服务同步 10.34 linux系统日志 10.35 scre

    10.32/10.33 rsync通过服务同步 10.34 linux系统日志 10.35 screen工具 转载于:https://blog.51cto.com/13227377/2116350

  6. 有博士点的计算机考研学校,河北省30所高校申报新增博士点33个,硕士点20个,有你母校吗?...

    原标题:河北省30所高校申报新增博士点33个,硕士点20个,有你母校吗? 河北省2020年博士硕士学位授权审核申报材料在省教育厅网站进行公示,河北全省共1所高校申报新增博士学位授权单位,12所高校申报 ...

  7. 没有bug队——加贝——Python 练习实例 31,32

    31.题目: 请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母. 程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母.用 if ...

  8. 蔡高厅老师 - 高等数学阅读笔记 - 06 - 高阶导数 隐函数- 02 (28、29、30、31)

    高级导数: 1 高阶导数 举例 利用数学归纳法假设 莱布尼兹公式, 29  隐函数.参量函数的导数 隐函数的导数: 显函数, 隐函数 不能显化的函数: 求导的方法: 斜率就是导数 或者表示为 取对数微 ...

  9. Effective C++ 条款30、31

    条款30 透彻了解inlining的里里外外 Inline函数,看起来像函数,动作像函数,比宏好得多,可以调用它们又不需要蒙受函数调用所招致的额外开销.但是,就像世界没有免费的午餐一样,inline的 ...

最新文章

  1. 使用appium做自动化测试时,send_keyss只能输入字母数字,无法输入中文
  2. 在JavaScript中使用inline函数的问题
  3. VC中的一些字符类型及其转换
  4. 修改linux系统iqn,linux iSCSI target配置全过程
  5. Django(三)框架之第二篇
  6. 利用双向循环链表实现长整数的存储_重学数据结构之链表篇
  7. php商品评价代码,php商品对比功能代码分享
  8. highlightjs 详解
  9. SVN 配置忽略文件
  10. PPT 设置幻灯片母版
  11. KTV房间一直显示连接服务器失败,KTV点歌服务器死机、卡歌、蓝屏的解决办法
  12. scrapy框架—spiders
  13. Pocket PC 2003 PC网卡上网设置
  14. java中如何插入表格_Java如何向Word文档中添加表格?
  15. 疯狂考证的两年:通过CFA、FRM、CMA经验与收获
  16. 我为什么鼓励你读博士?
  17. 小米手机Android内存管理基本情况介绍
  18. 关于链表的逆序排列!
  19. Statistics Foundations: 2 统计基础:2 Lynda课程中文字幕
  20. python 涨停统计_python 轻松实现获取涨跌停数据

热门文章

  1. git pull 配置免密_git 阿里云code免密pull push
  2. python语言应用 智慧树满分章节测试答案_2020知到答案 Python语言应用 最新智慧树满分章节测试答案...
  3. VLAN高级技术之VLAN聚合
  4. 关于TEA5767收音机模块以及TEA5767寄存器介绍。
  5. 【转】Flash mmorpg地图问题
  6. Windows安装mysql详细步骤(通俗易懂,简单上手)
  7. 爱上开源之golang入门至实战-使用IDE开发Golang
  8. VR 使用什么技术开发
  9. 计算机三级网络技术入口,全国计算机等级考试报名入口
  10. CF923B Producing Snow 堆/优先队列