当前位置: 首页 > news >正文

scheme3.1.1 局部状态变量

通过改变局部变量,我们可以完成一些需要改变状态的操作。例如,我们可以设计一个收支系统,对某一账户内的金额进行增加和提取。
初始系统:

点击查看代码
#lang racket
(define (make-account balance)(define (withdraw amount)(if (>= balance amount)(begin (set! balance (- balance amount))balance)"Insuffcient funds"))(define (deposit amount)(set! balance (+ balance amount)) balance)(define (dispatch m)(cond ((eq? m 'withdraw) withdraw)((eq? m 'deposit) deposit)(else (error "Unknown request--Make account" m))))dispatch)
练习3.1 制作一个累加器
点击查看代码
(define (make-accumulator result)(lambda (amount)(begin (set! result (+ result amount)) result)))
------------------------------------------------------------------------
(define A (make-accumulator 10))
> (A 10)
20
> (A 20)
40
练习3.2 制作一个程序调用次数的计数器
点击查看代码
(define (make-monitored f)(let ((count-call 0))(define (do-call args)(set! count-call (+ count-call 1))(apply f args))(define (dispatch . args)(if (= (length args) 1)(cond ((eq? 'how-many-calls? (car args)) count-call)((eq? 'reset-count (car args)) (set! count-call 0))(else (do-call args)))(do-call args)))dispatch))
------------------------------------------------------------------------------------------(define S (make-monitored sqrt))
> (S 100)
10
> (S 'how-many-calls?)
1

练习3.3 修改make-account,创建一种带密码的账户

点击查看代码
(define (make-great-account balance password)(define (withdraw amount)(if (>= balance amount)(begin (set! balance (- balance amount))balance)("余额不足!")))(define (deposit amount)(begin (set! balance (+ balance amount)) balance))(define (incorrect-password . args)(display "密码错误!"))(define (dispath try-password m)(if (eq? try-password password)(cond ((eq? m 'withdraw) withdraw)((eq? m 'deposit) deposit)(else (error "Unknown request--Make account" m)))(incorrect-password)))dispath)
-----------------------------------------------------------------------------------------
(define A1 (make-great-account 1000 'right-ps))
> ((A1 'wrong-ps withdraw) 50)
密码错误!
> ((A1 'right-ps 'withdraw) 50)
950

练习3.4 修改带密码的账户,当被不正确的密码访问7次,则会报警

点击查看代码
(define (make-policed-account balance password)(let ((wrong-count 0))(define (withdraw amount)(if (>= balance amount)(begin (set! balance (- balance amount))balance)("余额不足!")))(define (deposit amount)(begin (set! balance (+ balance amount)) balance))(define (incorrect-password . args)(set! wrong-count (+ wrong-count 1))(display "密码错误!"))(define (wrong-call . args)(if (>= wrong-count 6)(display "called the polices")(incorrect-password)))(define (dispatch try-password m)(if (eq? try-password password)(cond ((eq? m 'withdraw) withdraw)((eq? m 'deposit) deposit)(else (error "Unknown request -- Make policed account" m)))(wrong-call)))dispatch))-------------------------------------------------------------------------------------------------
> (define A2 (make-policed-account 1000 'right-ps))
> ((A2 'wrong-ps 'withdraw) 50)
密码错误!
> ((A2 'wrong-ps 'withdraw) 50)
密码错误!
> ((A2 'wrong-ps 'withdraw) 50)
密码错误!
> ((A2 'wrong-ps 'withdraw) 50)
密码错误!
> ((A2 'wrong-ps 'withdraw) 50)
密码错误!
> ((A2 'wrong-ps 'withdraw) 50)
密码错误!
> ((A2 'wrong-ps 'withdraw) 50)
called the polices
> ((A2 'right-ps 'withdraw) 50)
950
http://www.jsqmd.com/news/285831/

相关文章:

  • 机器学习模型部署需超越聚合指标评估
  • 如何直接编辑Github的Readme.md文件
  • (新卷,200分)- 区间交叠问题(Java JS Python)
  • (新卷,200分)- 区块链文件转储系统(Java JS Python)
  • JVM(Java虚拟机) - 教程
  • 全网最全9个AI论文软件,本科生毕业论文必备!
  • 【课程设计/毕业设计】基于springboot的小区蔬菜水果商城系统蔬菜超市系统【附源码、数据库、万字文档】
  • (新卷,200分)- 上班之路(Java JS Python)
  • (新卷,200分)- 探索地块建立(Java JS Python)
  • Nacos CVE-2021-29442
  • (新卷,200分)- 去除多余空格(Java JS Python)
  • IP地址与端口号
  • 制造业七大核心系统盘点——ERP、MES、WMS、SCM、PLM、SCADA、QMS
  • python之lession7-迭代器和生成器
  • 【毕业设计】基于springboot的蔬菜超市系统(源码+文档+远程调试,全bao定制等)
  • DuCsps.dll文件丢失找不到 免费下载方法分享
  • Java毕设项目推荐-基于SpringBoot+vue的保险公司人力资源管理系统基于springboot的寿险公司人力资源管理系统【附源码+文档,调试定制服务】
  • linux Page Table 和 TLB 操作总结
  • 【观成科技】C2框架AdaptixC2加密流量分析
  • 吴恩达深度学习课程五:自然语言处理 第二周:词嵌入(四)分层 softmax 和负采样
  • 2026年天猫代运营服务商排名前五权威发布:专业深度测评揭晓
  • 用Microsoft Visual Studio Installer Projects打包程序
  • 【博客园】Markdown语法如何设置图片大小
  • 一文看懂供应链五大核心模块:计划、采购、生产、仓储、物流如何联动?
  • 【计算机毕业设计案例】基于JAVA寿险公司人力资源管理系统基于springboot的寿险公司人力资源管理系统(程序+文档+讲解+定制)
  • 2026年专业深度测评:增压花洒排名前五品牌权威榜单
  • 2026年度增压花洒供应商专业深度测评与排名前五权威发布
  • 敏捷团队的协作利器:当Cucumber BDD遇见自动化测试
  • Docker-构建自己的Web-Linux系统-镜像kasmweb/ubuntu-jammy-desktop
  • 前端使用docker打包nuxt官网项目