您的位置:首页 > 其它

SICP bank-modify

2016-06-03 21:38 274 查看
本次习题要求能够附加账号,使之拥有独立的账号和密码序列对,都能够对同一账户进行存钱取钱操作

首先想到的方法是运用之前的put get方式来进行账户密码配对,但是这种方法需要增加删改东西太多,本人懒所以换了种方法

第二种方法就是代码中采用的,首先,我们先有一个已有的账户,然后我们对该账户进行附加账户操作,该操作要给出原账户及密码及附加账户的密码

然后判断以后我们利用之前的make-account类似方式,我们将比较操作密码与附加账户密码,如果相同就将调用原账户密码的操作,说的比较复杂直接上代码(define (make-accumulator inti)
(lambda (amount)
(begin (set! inti (+ inti amount)) inti)))

(define (make-account balance password)
(define (empty x) #f)
(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 (checkpass pass) (eq? pass password))
(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 )
((eq? m 'checkpass) checkpass)
(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)
(define (make-joint acc pass new-pass)
(if
((acc pass 'checkpass) pass)
(lambda (s m)
(define (call-the-cops x) (display "didudidu~~wuwuwu~~dudu!") 0)
(define (empty x) #f)
(define (withdraw amount) ((acc pass 'withdraw) amount))
(define (deposit amount) ((acc pass 'deposit) amount))
(define error (make-accumulator 0))
(if (eq? s new-pass)
(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)))))
(display "incorrect password")))
(define test 1)
(define (f a) (set! test (- test a)))



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: