亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
12下一頁
最近訪問板塊 發(fā)新帖
查看: 6113 | 回復(fù): 16
打印 上一主題 下一主題

SICP 習(xí)題參考答案 3.4 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2009-01-05 11:44 |只看該作者 |倒序瀏覽
此帖為 SICP 章節(jié) 3.4 的參考答案。為保持可讀性,請勿直接在本帖討論。討論請另開新帖。




[ 本帖最后由 win_hate 于 2009-1-5 11:45 編輯 ]

論壇徽章:
0
2 [報告]
發(fā)表于 2009-01-05 11:46 |只看該作者

Exercise 3.38.

Exercise 3.38.  Suppose that Peter, Paul, and Mary share a joint bank account that initially contains $100. Concurrently, Peter deposits $10, Paul withdraws $20, and Mary withdraws half the money in the account, by executing the following commands:
Peter:         (set! balance (+ balance 10))
Paul:         (set! balance (- balance 20))
Mary:         (set! balance (- balance (/ balance 2)))

a. List all the different possible values for balance after these three transactions have been completed, assuming that the banking system forces the three processes to run sequentially in some order.

b. What are some other values that could be produced if the system allows the processes to be interleaved? Draw timing diagrams like the one in figure 3.29 to explain how these values can occur.

論壇徽章:
0
3 [報告]
發(fā)表于 2009-01-05 11:49 |只看該作者

Exercise 3.38. 答案

  • + 和 - 連用時次序可交換,總共有四種可能:

    (100 + 10 - 20)/2 = 45
    (100 + 10)/2 - 20 = 35
    (100 - 10)/2 - 20 = 25
    (100/2 + 10 - 20) = 40
  • 假定沒個人的操作都可以分為兩步,訪問值,賦值。把 A 的兩個步驟記為 A1, A2,B,C 類似。

    可能的序列為 A1, A2, B1, B2, C1, C2 的一個排列,但其中 A1 在 A2 之前,B1 在 B2 之前,
    C1 在 C2 之前。

    可能的序列個數(shù)為:binomial(6,2)*binomial(4,2)=90

    其中一些序列生成的最終值是相同的。

    一個例子:A1 B1 B2 C1 C2 A2

    開始有 100 元。Paul 得到了 20, Mary 得到了 40,Peter 存入了 10 元。但銀行里現(xiàn)在有 110 元!

論壇徽章:
0
4 [報告]
發(fā)表于 2009-01-06 10:57 |只看該作者

Exercise 3.39.

Exercise 3.39.  Which of the five possibilities in the parallel execution shown above remain if we instead serialize execution as follows:

(define x 10)

(define s (make-serializer))

(parallel-execute (lambda () (set! x ((s (lambda () (* x x))))))
                  (s (lambda () (set! x (+ x 1)))))

論壇徽章:
0
5 [報告]
發(fā)表于 2009-01-06 10:59 |只看該作者

Exercise 3.39. 答案

第一個 lambda 中有兩個基本操作:

A) 訪問 x
B) 為 x 設(shè)置新值 x^2

第二個 lambda 整個是受保護(hù)的:

C) 為 x 設(shè)置新值 x+1

A, B 必定是順序出現(xiàn)的,所以可能時序只有三種:

C A B, A C B, A B C

對應(yīng)的值為:

121, 100, 101

[ 本帖最后由 win_hate 于 2009-1-7 09:03 編輯 ]

論壇徽章:
0
6 [報告]
發(fā)表于 2009-01-07 09:02 |只看該作者

Exercise 3.40.

Exercise 3.40.  Give all possible values of x that can result from executing

(define x 10)

(parallel-execute (lambda () (set! x (* x x)))
                  (lambda () (set! x (* x x x))))

Which of these possibilities remain if we instead use serialized procedures:

(define x 10)

(define s (make-serializer))

(parallel-execute (s (lambda () (set! x (* x x))))
                  (s (lambda () (set! x (* x x x)))))

論壇徽章:
0
7 [報告]
發(fā)表于 2009-01-07 09:04 |只看該作者

Exercise 3.40. 答案

設(shè)第一個 lambda 的兩個基本操作為 A1, A2,第二個 lambda 的兩個基本操作為 B1, B2。

可能的時序為有 6 個:

A1 A2 B1 B2 -> 1000000
A1 B1 A2 B2 -> 1000
A1 B1 B2 A2 -> 100
B1 A1 A2 B2 -> 1000
B1 A1 B2 A2 -> 100
B1 B2 A1 A2 -> 1000000

用 make-serializer 把兩個 lambda 完全保護(hù)起來后,A1 A2 是不可分的, B1 B2 也是。

所以可能的序列剩下兩個:

A1 A2 B1 B2 -> 1000000
B1 B2 A1 A2 -> 1000000

它們產(chǎn)生相同的最終效果。這是因為 (x^a)^b = (x^b)^a

論壇徽章:
0
8 [報告]
發(fā)表于 2009-01-09 10:33 |只看該作者

Exercise 3.41.

Exercise 3.41.  Ben Bitdiddle worries that it would be better to implement the bank account as follows (where the commented line has been changed):

(define (make-account balance)
  (define (withdraw amount)
    (if (>= balance amount)
        (begin (set! balance (- balance amount))
               balance)
        "Insufficient funds"))
  (define (deposit amount)
    (set! balance (+ balance amount))
    balance)
  ;; continued on next page

  (let ((protected (make-serializer)))
    (define (dispatch m)
      (cond ((eq? m 'withdraw) (protected withdraw))
            ((eq? m 'deposit) (protected deposit))
            ((eq? m 'balance)
             ((protected (lambda () balance)))) ; serialized
            (else (error "Unknown request -- MAKE-ACCOUNT"
                         m))))
    dispatch))

because allowing unserialized access to the bank balance can result in anomalous behavior. Do you agree? Is there any scenario that demonstrates Ben's concern?

[ 本帖最后由 win_hate 于 2009-1-9 10:36 編輯 ]

論壇徽章:
0
9 [報告]
發(fā)表于 2009-01-09 10:35 |只看該作者

Exercise 3.41. 答案

這樣做是多余的,因為查看余額不會修改 balance 變量。

論壇徽章:
0
10 [報告]
發(fā)表于 2009-01-09 10:36 |只看該作者

Exercise 3.42.

Exercise 3.42.  Ben Bitdiddle suggests that it's a waste of time to create a new serialized procedure in response to every withdraw and deposit message. He says that make-account could be changed so that the calls to protected are done outside the dispatch procedure. That is, an account would return the same serialized procedure (which was created at the same time as the account) each time it is asked for a withdrawal procedure.

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

Is this a safe change to make? In particular, is there any difference in what concurrency is allowed by these two versions of make-account ?
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP