练习3-1

原文

Exercise 3.1. An accumulator is a procedure that is called repeatedly with a single numeric argument and accumulates its arguments into a sum. Each time it is called, it returns the currently accumulated sum. Write a procedure make-accumulator that generates accumulators, each maintaining an independent sum. The input to make-accumulator should specify the initial value of the sum; for example
(define A (make-accumulator 5))
(A 10)
15
(A 10)
25

分析

这道题目并不难,其讲的是一个累加器,其实就和前面的银行余额那个差不多啦。而且比第153页的make-account更简单,这是因为这个函数只需要make-account的deposit一个函数而已。

代码

(define (make-accumulator value)(lambda (add-value)(set! value (+ value add-value))value))

练习3-2

原文

Exercise 3.2. In software-testing applications, it is useful to be able to count the number of times a given procedure is called during the course of a computation. Write a procedure make-monitored that takes as input a procedure, f, that itself takes one input. The result returned by make-monitored is a third procedure, say mf, that keeps track of the number of times it has been called by maintaining an internal counter. If the input to mf is the special symbol how-many-calls?, then mf returns the value of the counter. If the input is the special symbol reset-count, then mf resets the counter to zero. For any other input, mf returns the result of calling f on that input and increments the counter. For instance, we could make a monitored version of the sqrt procedure:
(define s (make-monitored sqrt))
(s 100)
10
(s ‘how-many-calls?)
1

分析

题目后的代码中很好的说明了过程make-monitored的作用,例如其传入参数sqrt后则会返回另一个过程给s。而将100这个参数传给s时则会得到100的sqrt。同时将how-many-calls?传给s,也就意味着将其传入make-monitored,这就是题目中所说的第二个参数。

因此对于make-monitored来说,
1.一个参数f
2.一个内部计数器count-call
3.返回一个过程,并且其有一个输入。
4.如果返回过程的输入为how-many-call?则返回count-call;
如果这个输入为reset-count则对count-call做清零处理;
如果是其他输入则将f应用于这个输入的结果,并将内部计数器加1,例如题目中的(s 10)。

而这段代码的难点在于,
1.一开始要用let将count-call记为0
2.在输入为reset-count的时,用set!将count-call清0
3.在其他情况时,除了用set!做加1处理外,还要同时将f应用于input。

代码

(define (make-monitored f)(let ((count-call 0))(lambda (input)(cond ((eq? input 'how-many-calls?) count-call)((eq? input 'reset-count) (begin (set! count-call 0) count-call))(else (begin (set! count-call (+ 1 count-call)) (f input)))))))

测试

(define (make-monitored f)(let ((count-call 0))(lambda (input)(cond ((eq? input 'how-many-calls?) count-call)((eq? input 'reset-count) (begin (set! count-call 0) count-call))(else (begin (set! count-call (+ 1 count-call)) (f input)))))))
;Value: make-monitored(define (cube x)(* x x x))
;Value: cube(define z (make-monitored cube))
;Value: z(z 10)
;Value: 1000(z 10)
;Value: 1000(z 'how-many-calls?)
;Value: 2

练习3-3

原文

Exercise 3.3. Modify the make-account procedure so that it creates password-protected accounts. That is, make-account should take a symbol as an additional argument, as in
(define acc (make-account 100 ‘secret-password))
The resulting account object should process a request only if it is accompanied by the password with which the account was created, and should otherwise return a complaint:
((acc ‘secret-password ‘withdraw) 40)
60
((acc ‘some-other-password ‘deposit) 50)
“Incorrect password”

分析

相对于153页的make-account函数,这道题要添加
1.判断密码是否正确的谓词correct-password?
2.输出错误信息的函数display-warning-message

另外在测试之后,会发现display-warning-message需要一个参数,即是它并没有用到。否则会有如下错误信息:

;The procedure #[compound-procedure 13 display-warning-message] has been called with 1 argument; it requires exactly 0 arguments.

代码

(define (make-account balance password)(define (withdraw amount)(if (>= balance amount)(begin (set! balance (- balance amount))balance)"Insufficient funds"))(define (deposit amount)(set! balance (+ balance amount)balance)(define (correct-password? input-password)(eq? password input-password))(define (display-warning-message msg)(display "Incorrect password"))(define (dispatch input-password m)(if (correct-password? input-password)(cond ((eq? m 'withdraw) withdraw) ((eq? m 'deposit) deposit) (else (error "Unknow request -- MAKE-ACCOUNT" mode)))display-warning-message))dispatch)

测试

(define acc (make-account 100 'secret-password))
;Value: acc((acc 'secret-password 'withdraw) 40)
;Value: 60((acc 'some-other-password 'deposit) 60)
Incorrect password
;Unspecified return value

练习3-4

原文

Exercise 3.4. Modify the make-account procedure of exercise 3.3 by adding another local state variable so that, if an account is accessed more than seven consecutive times with an incorrect password, it invokes the procedure call-the-cops.

分析

这道题是上一道题的补充,其主要添加了,
1.尝试次数try-times
2.上一题的display-warning-message做修改:用set!将尝试次数try-times加1,大于7次时调用过程call-the-cops

代码

(define (make-account balance password)(let ((try-times 0))(define (call-the-cops)(error "You try too much times, calling the cops ..."))(define (withdraw amount)(if (>= balance amount)(begin (set! balance (- blance amount)) balance)(define (deposit amount) (set! balance (+ balance amount)))(define (correct-password? input-password) (eq? password input-password))    (define (display-warning-message msg) (display "Incorrect password"))                        (define (dispatch input-password m) (if (password-match? input-password) (begin (set! try-times 0) (cond ((eq? m 'withdraw) withdraw) ((eq? m 'deposit) deposit) (else (error "Unknow request -- MAKE-ACCOUNT" mode)))) (begin (set! try-times (+ 1 try-times)) (if (>= try-times 7) (call-the-cops) display-warning-message)))) dispatch))

测试

(define acc (make-count 100 'secret-password));Value: acc((acc 'wrong-password 'withdraw) 10)
Incorrect password
;Unspecified return value((acc 'wrong-password 'withdraw) 10)
Incorrect password
;Unspecified return value((acc 'wrong-password 'withdraw) 10)
Incorrect password
;Unspecified return value((acc 'wrong-password 'withdraw) 10)
Incorrect password
;Unspecified return value((acc 'wrong-password 'withdraw) 10)
Incorrect password
;Unspecified return value((acc 'wrong-password 'withdraw) 10)
Incorrect password
;Unspecified return value((acc 'wrong-password 'withdraw) 10);You try too much times, calling the cops ...
;To continue, call RESTART with an option number:
; (RESTART 1) => Return to read-eval-print level 1.


感谢访问,希望对您有所帮助。 欢迎关注或收藏、评论或点赞。


为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp


【SICP练习】104 练习3.1-3.4相关推荐

  1. 零起点学算法104——第几天?

    零起点学算法104--第几天? Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lld Description 给定一个日期,输 ...

  2. 【tensorflow】OP_REQUIRES failed at variable_ops.cc:104 Already exists: Resource

    如下代码片段 outputs = tf.keras.layers.Bidirectional(tf.keras.layers.GRU(units=half_depth, use_bias=False, ...

  3. 腾讯绝地求生手游席卷全球,104个国家地区IOS登顶

    PUBG Mobile 腾讯旗下的PUBG Mobile在20日上线后,迅速成为世界级爆款游戏,在104个国家和地区登顶. PUBG Mobile 腾讯旗下的光子工作室这次可算是在全球的火爆了一把.这 ...

  4. 领扣-104/111 二叉树的最大深度 Maximum Depth of Binary Tree MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. csapp 、sicp 、深入理解计算机系统、 计算机程序的构造和解释

    CSAPP 第一版的英文版 深入理解计算机系统第一版中文版  这个是csdn账号  这里上传文件大小在10M以内  这个pdf是19+M的 深入理解计算机系统第二版的中文版下载 第一版英文版的介绍原书 ...

  6. SICP 习题 (2.7) 解题总结 : 定义区间数据结构

    SICP 习题 2.7 開始属于扩展练习,能够考虑不做,对后面的学习没什么影响.只是,假设上面的使用过程表示序对,还有丘奇计数你都能够理解的话,完毕这些扩展练习事实上没什么问题. 习题2.7是要求我们 ...

  7. 我的博客今天2岁104天了,我领取了…

    我的博客今天2岁104天了,我领取了徽章. 2011.06.09,我在新浪博客安家. 2011.06.09,我写下了第一篇博文:<看懂这些故事 你做人就很成功了>. 2011.06.09, ...

  8. 编写高质量代码改善C#程序的157个建议——建议104:用多态代替条件语句

    建议104:用多态代替条件语句 假设要开发一个自动驾驶系统.在设计之初,此自动驾驶系统拥有一个驾驶系统命令的枚举类型: enum DriveCommand{Start,Stop} 当前该枚举存在两个命 ...

  9. if the parser found inconsistent certificates on the files in the .apk.104

    当静默安装提示104时,是说升级的APK 和本地已经安装的APK 签名不一致,所以无法升级. 经百度,找到知乎同学@陈子腾的回答,找到了问题所在. 可以比对apk签名的fingerprint. 假定安 ...

最新文章

  1. perl5 第十章 格式化输出
  2. PMCAFF问答精选 | 程序员转型产品经理真的明智吗?
  3. JS对象变量、闭包的一些问题
  4. 洛谷 P1273 【有线电视网】
  5. 题目:社区人员登记管理系统(有源码链接免费下载)
  6. Lucene学习总结之八:Lucene的查询语法,JavaCC及QueryParser
  7. PostgreSQL 数据类型
  8. [react] React中如何监听state的变化?
  9. 想都不敢想!这8个神奇“黑科技”原来已经有人弄出来了
  10. 中随机打乱序列的函数_excel函数应用:如何快速制作考生座次分配表
  11. 备忘: Visual Studio 2013 VC++ IDE 使用小贴示。
  12. 在演示文稿中控制视频播放效果
  13. struts+spring action应配置为scope=prototype
  14. 基于Django图书管理系统设计与实践
  15. 潇洒老师教你注塑模具使用顶针油需要注意的问题
  16. 机器学习(九)决策树,随机森林
  17. 我94年的,做了一年外包我就跑路了
  18. mysql 行转列case when_mysql行转列利用casewhen_MySQL
  19. Ubuntu-拼音输入法安装
  20. 闲谈|如何查阅行业报告和行业数据?

热门文章

  1. arm b bl 地址无关码_ARM_异常和中断
  2. java future用法_Java中的多线程知识点
  3. hashmap为什么用红黑树_关于HashMap的实现,一篇文章带你彻底搞懂,再也不用担心被欺负
  4. Python大佬抓取了招聘信息并告诉你哪种Python 程序员最赚钱
  5. android 中自定义安装,AndroidStudio 自定义配置
  6. word2vec相似度计算_图解word2vec(原文翻译)
  7. python的csv模块的write_rows_Python3使用csv模块csv.writer().writerow()保存csv文件,产生空行的问题...
  8. 企业IT运维的“安全终结者”-堡垒机指南
  9. JavaScript初学者编程题(2)
  10. 关于学习tf.random.normal()和tf.random.uniform()的一点小总结