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)))
首先想到的方法是运用之前的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)))
相关文章推荐
- 堆结构的C++模板类实现
- 图像处理的算法SIFT具有尺度不变性
- spring aop记录操作日志和错误日志
- JavaScript创建对象的几种模式
- .length()与.length与.size()
- 强悍的 ubuntu —— 命令行执行完毕通知(undistract-me)
- 剑指Offer----面试题14:调整数组顺序使奇数位于偶数前面
- gcd实现单例模式其一
- 我写的那六本开源计算机书
- am335x_gpio
- 源码解读--知乎日报
- angular实现window.onload和$(document).ready
- Java并发包概览
- 强烈推荐 | 百度、阿里、腾讯 40 道面试题超全面总结
- simple-spring-memcached缓存搭建
- U-BOOT移植心得
- 使用ZooKeeper实现Java跨JVM的分布式锁(读写锁)
- 《将博客搬至CSDN》
- windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTim
- mysql主从复制配置(精简版)