<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 閱讀(1621) 評論(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))



    主站蜘蛛池模板: 成年18网站免费视频网站| 欧洲精品成人免费视频在线观看 | 亚洲偷自拍拍综合网| 亚洲中文字幕伊人久久无码| 精品久久久久久久久亚洲偷窥女厕| 本道天堂成在人线av无码免费| 国产jizzjizz视频免费看| 国产精品亚洲专一区二区三区| 国产精品久久香蕉免费播放| 日本永久免费a∨在线视频| 亚洲国产成人久久一区WWW| www一区二区www免费| 亚洲国产成人片在线观看无码| 最新亚洲春色Av无码专区 | 免费a级毛片无码a∨性按摩| 亚洲午夜精品一区二区| 在线观看成人免费视频不卡| 亚洲最大中文字幕无码网站| 国产高清免费的视频| a级毛片免费观看网站| 亚洲国产精品无码专区在线观看| 日韩在线不卡免费视频一区| 亚洲精品美女网站| 亚洲av无码天堂一区二区三区| 中国一级全黄的免费观看| 亚洲嫩草影院久久精品| 噼里啪啦电影在线观看免费高清 | 伊人久久亚洲综合影院| 亚洲av无码片区一区二区三区| 大学生高清一级毛片免费| 一个人看的www免费在线视频| 亚洲bt加勒比一区二区| 97无码免费人妻超级碰碰夜夜| 四虎影视久久久免费| 久久丫精品国产亚洲av不卡| 最近免费中文字幕大全视频| 国产福利免费视频| 天堂亚洲国产中文在线| 亚洲乱色熟女一区二区三区丝袜| 黄 色一级 成 人网站免费| 亚洲综合综合在线|