【SICP练习】107 练习3.8

简介:

练习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: 0

m
;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


目录
相关文章
|
14天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
6天前
|
云安全 人工智能 安全
Dify平台集成阿里云AI安全护栏,构建AI Runtime安全防线
阿里云 AI 安全护栏加入Dify平台,打造可信赖的 AI
|
9天前
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
838 25
|
8天前
|
机器学习/深度学习 人工智能 搜索推荐
万字长文深度解析最新Deep Research技术:前沿架构、核心技术与未来展望
近期发生了什么自 2025 年 2 月 OpenAI 正式发布Deep Research以来,深度研究/深度搜索(Deep Research / Deep Search)正在成为信息检索与知识工作的全新范式:系统以多步推理驱动大规模联网检索、跨源证据。
576 46
|
2天前
|
监控 BI 数据库
打工人救星!来看看这两家企业如何用Quick BI让业务更高效
Quick BI专业版监控告警助力企业高效运作,通过灵活配置规则与多渠道推送,让数据异常早发现、快响应,推动业务敏捷决策与持续增长。
打工人救星!来看看这两家企业如何用Quick BI让业务更高效
|
8天前
|
人工智能 Java Nacos
基于 Spring AI Alibaba + Nacos 的分布式 Multi-Agent 构建指南
本文将针对 Spring AI Alibaba + Nacos 的分布式多智能体构建方案展开介绍,同时结合 Demo 说明快速开发方法与实际效果。
562 41