要求创建一个简易的银行账户管理系统
这里采用了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)