练习3-44
原文
Exercise 3.44. Consider the problem of transferring an amount from one account to another. Ben Bitdiddle claims that this can be accomplished with the following procedure, even if there are multiple people concurrently transferring money among multiple accounts, using any account mechanism that serializes deposit and withdrawal transactions, for example, the version of make-account in the text above.
(define (transfer from-account to-account amount)
((from-account 'withdraw) amount)
((to-account 'deposit) amount))
Louis Reasoner claims that there is a problem here, and that we need to use a more sophisticated method, such as the one required for dealing with the exchange problem. Is Louis right? If not, what is the essential difference between the transfer problem and the exchange problem? (You should assume that the balance in from-account is at least amount.)
分析
题目中的transfer和第214页的exchange有一个很大的区别,前者不仅仅是计算余额的增减,还要计算它们的差值(difference),而在transfer中则不需要,只有一个amount,并且这个amount只是在每次进行增减余额之后更新。
transfer中,((from-account ‘withdraw) amount)和((to-account ‘deposit) amount)都已经进行了串行化因此都会逐个正确执行。但是在一方减少余额之后,另一方增加余额与其他的操作的先后顺序则不能肯定。但增减余额这两个操作都能始终被执行。
感谢访问,希望对您有所帮助。 欢迎关注或收藏、评论或点赞。
为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp