SICP banksystem

要求创建一个简易的银行账户管理系统
这里采用了set!函数来改变参数balance 并将其作为Local variable 使得无法被外部访问
之后我们利用类似的方式实现一个计数器,将其计数密码错误次数,当次数达到一定以后就报警
难度不大只是这里的局部变量有些难以理解,为何会保留之前的参数,实在需要加深理解
以下是本题代码
(define (make-accumulator inti)
	(lambda (amount) 
		(begin (set! inti (+ inti amount)) inti)))

(define (make-account balance password)
	(define (empty x) ())
	(define (withdraw amount)
		(if	(>= balance amount)
			(begin (set! balance (- balance amount)) balance)
			(begin (display "Not enough") balance)))
	(define (deposit amount)
		(begin (set! balance (+ balance amount )) balance))	
	(define error (make-accumulator 0))
	(define (dispatch s m)
		(define (call-the-cops x) (display "didudidu~~wuwuwu~~dudu!") 0)
		(if	(eq? s password)
			(cond
				((eq? m 'withdraw) withdraw)
				((eq? m 'deposit ) deposit )
				(else (begin (display "Error Operation!") empty)))
			(begin 	(display "incorrect pass")
				(display "\n")
				(error 1)
				(if	(= (error 0) 3)
					call-the-cops
					(begin (display (error 0)) empty)))))
	dispatch)

(define (make-monitored f)
	(define count (make-accumulator 0))
	(define (dispatch m)
		(cond
			((eq? m 'how-many-calls) (count 0))	
			((number? m) (begin (count 1) (f m)))
			(else (error "Error operation!"m))))
	dispatch)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值