首页 > 其他 > 详细

【SICP练习】118 练习3.45

时间:2015-03-26 23:38:30      阅读:361      评论:0      收藏:0      [点我收藏+]

练习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.

代码

待完成

【SICP练习】118 练习3.45

原文:http://blog.csdn.net/nomasp/article/details/44654825

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!