【SICP练习】118 练习3.45【待完成】

本文深入分析了Louis提出的改进银行账户系统的想法,指出其逻辑上的错误,并详细解释了当使用序列化交换时会发生的问题。通过具体代码示例,揭示了Louis提议的实现方式存在的潜在风险。

练习3-45

原文

Exercise 3.45. Louis Reasoner thinks our bank-account system is unnecessarily complex and error-prone now that deposits and withdrawals aren’t automatically serialized. He suggests that make-accountand-serializer should have exported the serializer (for use by such procedures as serializedexchange) in addition to (rather than instead of) using it to serialize accounts and deposits as makeaccount did. He proposes to redefine accounts as follows:

 (define (make-account-and-serializer balance) 
    (define (withdraw amount)   
       (if (>= balance amount)     
           (begin (set! balance (- balance amount))                
                   balance)       
            "Insufficient funds")) 
    (define (deposit amount)  
       (set! balance (+ balance amount))
       balance) 
    (let ((balance-serializer (make-serializer)))   
       (define (dispatch m)  
          (cond ((eq? m 'withdraw) (balance-serializer withdraw))            
                ((eq? m 'deposit) (balance-serializer deposit))            
                ((eq? m 'balance) balance)    
                ((eq? m 'serializer) balance-serializer)            
                (else (error "Unknown request -- MAKE-ACCOUNT" m))))  
        dispatch))

Then deposits are handled as with the original make-account:

(define (deposit account amount) 
   ((account 'deposit) amount))

Explain what is wrong with Louis’s reasoning. In particular, consider what happens when serializedexchange is called.

代码

临时发现有误,待完成。



感谢访问,希望对您有所帮助。 欢迎关注或收藏、评论或点赞。


为使本文得到斧正和提问,转载请注明出处:
http://blog.youkuaiyun.com/nomasp


版权声明:本文为 NoMasp柯于旺 原创文章,如需转载请联系本人。

转载于:https://www.cnblogs.com/NoMasp/p/4786083.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值