练习3-8

原文

Exercise 3.8. When we defined the evaluation model in section 1.1.3, we said that the first step in evaluating an expression is to evaluate its subexpressions. But we never specified the order in which the subexpressions should be evaluated (e.g., left to right or right to left). When we introduce assignment, the order in which the arguments to a procedure are evaluated can make a difference to the result. Define a simple procedure f such that evaluating

(+ (f 0) (f 1)) 

will return 0 if the arguments to + are evaluated from left to right but will return 1 if the arguments are evaluated from right to left.

分析

题目中已经说明了求解一个表达式的第一步就是求值其中的子表达式,而对于

(+ (f 0) (f 1)) 

意味着分别求解(f 0)和(f 1),可以猜测着其中有2个lambda表达式。又因为从左往右和从右往左的求值结果不同,则意味着2个lambda是嵌套的关系。

代码

(define f(lambda (first-value)(set! f (lambda (second-valua) 0))first-value))
;Value: 

测试

(define f(lambda (first-value)(set! f (lambda (second-valua) 0))first-value))
;Value: f(+ (f 0) (f 1))
;Value: 1(+ (f 1) (f 0))
;Value: 0

总结

当我们调用(f n)时,n为实参将会代替第四行的first-value,并进一步传入第二行的lambda表达式。返回的结果则是n,但与此同时又将

(lambda (second-value) 0)

赋值给过程f。而当下一次调用(f m)时,不论m为何值,都会返回0。也就是说第一次传入的n,(f n)返回值为n,以后传入的m,(f m)返回0,并且无论传入多少次m,返回值均为0。当然了,m的值并未改变。

(f 1)
;Value: 1(f 1)
;Value: 0(f 1)
;Value: 0(f 1)
;Value: 0(define m 2)
;Value: m(f m)
;Value: 0m
;Value: 2

同时也可以得出结论,MIT-Scheme对子表达式的求值顺序是从右至左。

练习3-7

原文

Exercise 3.7. Consider the bank account objects created by make-account, with the password modification described in exercise 3.3. Suppose that our banking system requires the ability to make joint accounts. Define a procedure make-joint that accomplishes this. Make-joint should take three arguments. The first is a password-protected account. The second argument must match the password with which the account was defined in order for the make-joint operation to proceed. The third argument is a new password. Make-joint is to create an additional access to the original account using the new password. For example, if peter-acc is a bank account with password open-sesame, then

(define paul-acc (make-joint peter-acc 'open-sesame 'rosebud))

will allow one to make transactions on peter-acc using the name paul-acc and the password rosebud. You may wish to modify your solution to exercise 3.3 to accommodate this new feature.

分析

make-joint需要有3个参数:
1.有密码保护的帐户名
2.必须与账号的密码匹配的原密码
3.新密码

而其会返回一个过程,因此在此处需要一个lambda表达式,并且其有一个参数mode和一个传入的密码参数。另外在输出错误信息的函数中也需要一个参数,即是它并不使用,只是出于兼容性的考虑,在前面的博客中我们也遇到过这种问题。

代码

(define (make-joint origin-acc old-password new-password)(define (display-wrong-message msg)(display "Incorrect password"))(lambda (given-password mode)(if (eq? given-password new-password)(origin-acc old-password mode)display-wrong-message)))
;Value: make-joint


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


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


版权声明:本文为 NoMasp柯于旺 原创文章,如需转载请联系本人。

转载于:https://www.cnblogs.com/NoMasp/p/4786098.html

【SICP练习】107 练习3.8相关推荐

  1. 全国各城市经纬度,代码等

    参考资料,转载出处: 1.http://www.smsyun.com/home-index-page-id-42.html 2.http://blog.csdn.net/a497785609/arti ...

  2. SICP(计算机程序构造与解释)学习笔记(lisp语言实现)

    用的是这版本 由于逆天的语法,必须准确记清楚 目录 Scheme语法 递归与阶乘 1.递归与阶乘的区别 2.阶乘与递归的实例 阶乘的递归实现 阶乘的迭代实现 斐波那契数列的递归实现 1.3高阶过程实现 ...

  3. Android 3.0 r1 API中文文档(107) —— AsyncPlayer

    一.结构 public class AsyncPlayer extends Object java.lang.Object android.media.AsyncPlayer 二.概述 播放一个连续( ...

  4. SAP VLPOD 报错 - Update control of movement type is incorrect (entry 107 X X E B _ E) - 之对策

    SAP VLPOD 报错 - Update control of movement type is incorrect (entry 107 X X E B _ E) - 之对策 如下的DN 8002 ...

  5. android 图片 编辑app,图片编辑工具手机版下载-图片编辑工具app下载8.33.107安卓官方版-西西软件下载...

    图片编辑工具app,非常实用的手机端图片处理工具!有时候一些图片简单的修改,还要打开电脑,连接手机传到电脑上相关软件进行改动,最后再传回手机,如果没电脑,难不成还等到有电脑的时候才能解决这个问题吗?现 ...

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

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

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

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

  8. (水题)987654321 problem -- SGU 107

    链接: http://vj.acmclub.cn/contest/view.action?cid=168#problem/G 时限:250MS     内存:4096KB     64位IO格式:%I ...

  9. ssh连接服务器出现:ssh: connect to host 192.168.1.107 port 22: Connection refused 的解决方法

    文章目录: 1 说明遇到问题场景 2 解决方式 1 说明遇到问题场景 1.我的系统环境 windows10 连接的服务器系统为:Mint19.3 2.我使用windows,在局域网下通过ssh连接服务 ...

最新文章

  1. 基于AOP的事务管理与普通事务管理有什么区别?
  2. 搜索推荐炼丹笔记:融合GNN、图谱、多模态的推荐
  3. 网络防火墙实战-基于pfsense(1)
  4. vue.js的一些小语法v-bind,v-if,v-show,v-else
  5. 中国科学院大学庆生 一颗小行星以“国科大”命名
  6. python 按钮控件_python实现360皮肤按钮控件示例
  7. 集成Tomcat环境到Eclipse中
  8. FTP文件传输协议介绍和常用命令
  9. 100个2022实用微信小程序源码分享
  10. 一次破解TP-Link WAR308路由器的经历(2)
  11. 拉格朗日方程的三种推导方法之基于汉密顿原理推导
  12. Python批量爬取堆糖网图片
  13. 3w最简单led灯电路图_一款简单实用的LED灯驱动电路
  14. ORA-12569: TNS: 包校验和失败解决方法一例
  15. Linux+conda+R+Rstudio下载安装环境全方面配置
  16. c++--stack,queue,priority_queue
  17. MySQL实现按距离范围查找
  18. 不自律的人,如何把一件事做成功?
  19. 操作系统真象还原[11章]-用户进程
  20. Sql server 行列转换 PIVOT UNPIVOT

热门文章

  1. 使用OpenCV进行人脸识别的三种方法
  2. Github新建分支以处理原仓库提交时detached HEAD的问题
  3. Oracle的PL/SQL编程前奏之基础技能实战一(匿名子程序)
  4. 网狐荣耀版通过水浒传基础二开埃及拉霸和水果森林步骤
  5. 05-RARP: 逆地址解析协议
  6. HTTP权威指南阅读笔记五:Web服务器
  7. ubuntu16禁用utc时间
  8. 让底部始终在浏览器底部
  9. Tiles Framework
  10. 高档名片设计:12款专业的名片设计欣赏