<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))



    主站蜘蛛池模板: 国产成人精品日本亚洲专区61| 亚洲Av无码专区国产乱码DVD| 日韩电影免费在线观看网址 | 亚洲精品在线免费观看| 亚洲欧美国产日韩av野草社区| 亚洲国产成人精品无码久久久久久综合 | 日韩电影免费在线| 两个人的视频www免费| 亚洲国产片在线观看| 亚洲国产综合无码一区二区二三区 | 亚洲另类无码专区丝袜| 亚洲αv久久久噜噜噜噜噜| 插B内射18免费视频| 91视频精品全国免费观看| 一本色道久久综合亚洲精品蜜桃冫 | 黄页网址在线免费观看| 亚洲欧洲日产国码www| 亚洲色一色噜一噜噜噜| 最近最新的免费中文字幕 | 四虎影永久在线高清免费| 18观看免费永久视频| 一级毛片aaaaaa视频免费看| 亚洲制服丝袜第一页| 亚洲Aⅴ无码专区在线观看q| 内射无码专区久久亚洲| 成人免费一区二区三区在线观看| 日韩精品无码免费专区午夜不卡 | 亚洲黄色免费网址| 国产V片在线播放免费无码| 亚洲熟妇AV日韩熟妇在线| 亚洲无删减国产精品一区| 日韩精品亚洲aⅴ在线影院| 日韩成人在线免费视频| 精品福利一区二区三区免费视频| 三年片免费高清版| 日韩精品视频在线观看免费| 亚洲人成77777在线观看网| 亚洲人成在线播放网站岛国| 亚洲国产成人一区二区精品区 | 亚洲国语精品自产拍在线观看| 亚洲高清国产拍精品青青草原|