<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    莊周夢蝶

    生活、程序、未來
       :: 首頁 ::  ::  :: 聚合  :: 管理

    sicp 4.3.2部分習題

    Posted on 2008-11-15 00:02 dennis 閱讀(1613) 評論(0)  編輯  收藏 所屬分類: 計算機科學與基礎

    4.38,謎題就有翻譯錯誤,問題更是錯的離譜。原題是這樣的:
    Baker, Cooper, Fletcher, Miller, and Smith live on different floors of an apartment house
    that contains only five floors. Baker does not live on the top floor. Cooper does not live on
    the bottom floor. Fletcher does not live on either the top or the bottom floor. Miller lives on
    a higher floor than does Cooper. Smith does not live on a floor adjacent to Fletcher's.
    Fletcher does not live on a floor adjacent to Cooper's. Where does everyone live?

    其中說Miller住的比Cooper高,卻翻譯成了Miller住的比Cooper高一層,如果按照這個翻譯來謎題是沒有答案的。
    回到4.38,題目是這樣的:
    Modify the multiple-dwelling procedure to omit the requirement that Smith and Fletcher do
    not live on adjacent floors. How many solutions are there to this modified puzzle?

    翻譯卻成了增加Smith和Fletcher不住在相鄰層這個條件,謎題本來就是如此,何來的增加?錯將omit翻譯成了“增加”。
    這道題很簡單咯,將
    (require (not (= (abs (- smith fletcher)) 1)))
    注釋掉即可,答案增加到5個:
    > (multiple-dwelling)
    ((baker 
    1) (cooper 2) (fletcher 4) (miller 3) (smith 5))
    > (amb)
    ((baker 
    1) (cooper 2) (fletcher 4) (miller 5) (smith 3))
    >  (amb)
    ((baker 
    1) (cooper 4) (fletcher 2) (miller 5) (smith 3))
    >  (amb)
    ((baker 
    3) (cooper 2) (fletcher 4) (miller 5) (smith 1))
    >  (amb)
    ((baker 
    3) (cooper 4) (fletcher 2) (miller 5) (smith 1))

    4.39,約束條件的順序肯定是有影響的,能縮小搜索范圍的強約束條件排在前面,弱約束條件排在后面,可以減少整體的判斷次數。在DrScheme中,可以啟用profile來分析順序帶來的影響,打開language->R5RS->Show Details,選擇Debugging and Profiling 即可。運行scheme程序,然后在View->Show Profile中查看具體分析結果,在該視圖中將詳細列出各個函數調用的時間和次數。
    在沒有調整順序前:
    Msec      Calls      Function
    40            1               multiple-dwelling
    0              1716         require
    4              2524          distinct?

    說明multiple-dwelling調用了一次,花費了40毫秒,而require和distinct?函數分別被調用了1716次和2524次。
    然后我將
    (require
         (distinct? (list baker cooper fletcher miller smith)))

    這個我認為弱約束條件放到了最后,測試的結果并不讓人滿意:
    Msec      Calls      Function
    44            1               multiple-dwelling
    4              6035         require
    0              129          distinct?

    并沒有大的提高,甚至反而所下降。猜測問題在于我使用的amb實現是call/cc、宏實現的,待俺完成amb求值器再測試一下。

    4.40,題目都提示咯,嵌套let語句,我的解答:
    (define (multiple-dwelling2)
      (let ((baker   (amb 
    1 2 3 4 5)))
         (require (not (
    = baker 5)))
          (let ((cooper  (amb 
    1 2 3 4 5)))
            (require (not (
    = cooper 1)))
            (let ((fletcher (amb 
    1 2 3 4 5)))
              (require (not (
    = fletcher 5))) 
              (require (not (
    = fletcher 1)))
              (require (not (
    = (abs (- fletcher cooper)) 1)))
              (let ((miller (amb 
    1 2 3 4 5)))
                 (require (
    > miller cooper))
                 (let ((smith   (amb 
    1 2 3 4 5)))
                    (require (not (
    = (abs (- smith fletcher)) 1)))
                    (require (distinct
    ? (list baker cooper fletcher miller smith)))
                    (list (list 
    'baker baker)
                          (list 'cooper cooper)
                          (list 'fletcher fletcher)
                          (list 'miller miller)
                          (list 'smith smith))))))))

    profile一下,multiple-dwelling2的調用時間縮小到8毫秒,require和distinct?的調用次數分別降低到了404和129次。


    4.42,說謊者謎題,
    五個女生參加一個考試,她們的家長對考試結果過分關注。為此她們約定,在給家里寫信談到考試的時候,每個姑娘都要寫一句真話和一句假話。下面是從她們的信里摘抄出來的句子:
    Betty : kitty考第二,我只考了第三
    Ethel : 你們應該很高興聽到我考了第一,joan第二
    joan :   我考第三,可憐的Ethel墊底
    kitty:  我第二,marry只考了第四
    marry: 我是第四,Betty的成績最高。
    這五個姑娘的實際排名是什么?

    將題目翻譯成代碼就OK了,說明性編程真是舒坦:
    (define (liars-puzzle)
      (let ((betty (amb 
    1 2 3 4 5))
            (ethel (amb 
    1 2 3 4 5))
            (joan (amb 
    1 2 3 4 5))
            (kitty (amb 
    1 2 3 4 5))
            (marry (amb 
    1 2 3 4 5)))
        (require
           (distinct
    ? (list betty ethel joan kitty marry)))
        (require (or (
    = kitty 2) (= betty 3)))
        (require (not (and (
    = kitty 2) (= betty 3))))
        (require (or (
    = ethel 1) (= joan 2)))
        (require (not (and (
    = ethel 1) (= joan 2))))
        (require (or (
    = joan 3) (= ethel 5)))
        (require (not (and (
    = joan 3) (= ethel 5))))
        (require (or (
    = kitty 2) (= marry 4)))
        (require (not (and (
    = kitty 2) (= marry 4))))
        (require (or (
    = marry 4) (= betty 1)))
        (require (not (and (
    = marry 4) (= betty 1))))
        (list (list 
    'betty betty)
              (list 'ethel ethel)
              (list 'joan joan)
              (list 'kitty kitty)
              (list 'marry marry))))

    答案是:
    ((betty 3) (ethel 5) (joan 2) (kitty 1) (marry 4))

    4.43.也很有趣的題目,游艇迷題,
       Mary Ann Moore的父親有一條游艇,他的四個朋友Colonel Dowing、Mr.Hall、Sir Barnacle Hood和Dr.Parker也各有一條。這五個人都有一個女兒,每個人都用另一個人的女兒的名字來為自己的游艇命名。Sir Barnacle的游艇叫Gabrielle,Mr.Moore擁有Lorna,Mr.Hall的游艇叫Rosalind,Melissa屬于Colonel Downing(取自Sir Barnacle的女兒的名字),Garielle的父親的游艇取的是Dr.Parker的女兒的名字。請問,誰是Lorna的父親。

    先說下結果,Lorna的父親是Downing。
    具體解答如下,先定義輔助過程:
    (define (father yacht daughter)
         (cons yacht daughter))
    (define (yacht father)
      (car father))
    (define (daughter father)
      (cdr father))

    然后翻譯題目為代碼即可,暫不考慮效率問題:
    (define (yacht-puzzle)
      (let ((moore (father 
    'lorna 'mary))  ;;Mr.Moore
            (downing (father 
    'melissa (amb 'mary 'melissa 'gabrielle 'rosalind 'lorna))))  ;;Colonel Downing
        (require (not (equal
    ? (yacht downing) (daughter downing))))
        (let ((hall (father 
    'rosalind (amb 'mary 'melissa 'gabrielle 'rosalind 'lorna))))  ;;Mr.Hall
           (require (not (equal
    ? (yacht hall) (daughter hall))))
           (let ((barnacle (father 
    'gabrielle 'melissa))   ;;Sir Barnacle Hood
                 (parker (father (amb 
    'mary 'melissa 'gabrielle 'rosalind 'lorna) (amb 'mary 'melissa 'gabrielle 'rosalind 'lorna))))         ;;Dr.Parker
             (require (not (equal
    ? (yacht parker) (daughter parker))))
             (let ((gabrielle
    -father (amb moore downing hall barnacle parker))) ;;Garielle's Father
               (require (equal? (daughter gabrielle-father) 'gabrielle))   
               (require (equal? (yacht gabrielle-father) (daughter parker)))
               (require (distinct
    ? (map daughter (list moore downing hall barnacle parker))))
               (require (distinct
    ? (map yacht (list moore downing hall barnacle parker))))
               (list 
                  (list 
    'moore (yacht moore) (daughter moore))
                  (list 'downing (yacht downing) (daughter downing))
                  (list 'hall (yacht hall) (daughter hall))
                  (list 'barnacle (yacht barnacle) (daughter barnacle))
                  (list 'parker (yacht parker) (daughter parker))))))))

    運行(yacht-puzzle)的結果為:
    ((moore lorna mary) (downing melissa lorna) (hall rosalind gabrielle) (barnacle gabrielle melissa) (parker mary rosalind))

    三元組:父親名、游艇名、女兒名,因此lorna的父親是Downing。Garielle的父親是Mr.Hall,Mr.Hall的游艇名叫做Rosalind,正是Dr.Parker的女兒名字。

    延伸題目,如果沒有Mary Ann姓Moore這個條件,答案將有三個,分別是:
    ((moore lorna mary) (downing melissa lorna) (hall rosalind gabrielle) (barnacle gabrielle melissa) (parker mary rosalind))
    ((moore lorna gabrielle) (downing melissa rosalind) (hall rosalind mary) (barnacle gabrielle melissa) (parker mary lorna))
    ((moore lorna lorna) (downing melissa mary) (hall rosalind gabrielle) (barnacle gabrielle melissa) (parker mary rosalind))



    主站蜘蛛池模板: 亚洲一区无码中文字幕| 免费观看男人免费桶女人视频 | 亚洲精品456播放| 日本亚洲成高清一区二区三区 | 亚洲资源在线视频| 亚洲性色精品一区二区在线| 亚洲欧洲AV无码专区| 国产精品小视频免费无限app| 99在线免费视频| 亚洲w码欧洲s码免费| 免费在线观看视频a| 亚洲最新黄色网址| 免费国产va在线观看| 在线免费观看亚洲| 亚洲精品无码日韩国产不卡?V| 免费无码专区毛片高潮喷水| 亚洲伊人成无码综合网 | 少妇人妻偷人精品免费视频| 成人免费777777| 国产亚洲高清不卡在线观看| 日本系列1页亚洲系列| 亚洲一区免费观看| 亚洲国产理论片在线播放| 久久国产精品免费一区二区三区| 免费看又爽又黄禁片视频1000| 国产亚洲精品美女2020久久| 最新猫咪www免费人成| 久久精品国产亚洲AV大全| 亚洲一区二区三区免费| 亚洲AV无码专区在线播放中文| 四虎成人精品国产永久免费无码| 成人免费一区二区三区在线观看| 亚洲av无码日韩av无码网站冲| 啦啦啦高清视频在线观看免费| 亚洲AV无码成人精品区天堂| 亚洲免费闲人蜜桃| 看Aⅴ免费毛片手机播放| 亚洲av永久无码精品表情包| 嫩草影院在线免费观看| 中文字幕免费在线播放| 亚洲熟女少妇一区二区|