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

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

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

    莊周夢蝶

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

    sicp 習題1.31,1.32,1.33解答

    Posted on 2007-05-14 15:10 dennis 閱讀(698) 評論(0)  編輯  收藏
        此題與1.32、1.33是一個系列題,沒什么難度,只不過把sum的加改成乘就可以了,遞歸與迭代版本相應修改即可:
    ;(define (product term a next b)
    ;  (
    if (> a b)
    ;      
    1
    ;      (
    * (term a) (product term (next a) next b))))
    (define (product
    -iter term a next b result)
      (
    if (> a b)
          result
          (product
    -iter term (next a)  next b (* result (term a)))))
       分號注釋的是遞歸版本。利用product過程生成一個計算pi的過程,也是很簡單,通過觀察公式的規律即可得出:
    (define (product term a next b)
      (product
    -iter term a next b 1))
    (define (inc x) (
    + x 2))
    (define (pi
    -term n)(/ (* (- n 1) (+ n 1)) (* n n)))
    (define (product
    -pi a b)
       (product pi
    -term a inc b))
    測試一下:

    > (* 4 (product-pi 3 1000))
    3.1431638424191978569077933

    再來看習題1.32,如果說sum和product過程是一定程度的抽象,將對累積項和下一項的處理抽象為過程作為參數提取出來,那么這個題目要求將累積的操作也作為參數提取出來,是更高層次的抽象,同樣也難不倒我們:
    (define (accumulate combiner null-value term a next b)
      (
    if (> a b)
          null
    -value
          (combiner (term a) (accumulate combiner null
    -value term (next a) next b))))

    OK,其中combiner是進行累積的操作,而null-value值基本值。現在改寫sum和product過程,對于sum過程來說,累積的操作就是加法,而基本值當然是0了:
    (define (sum term a next b)
      (accumulate 
    + 0 term a next b))

    而對于product,累積操作是乘法,而基本值是1,因此:
    (define (product term a next b)
      (accumulate 
    * 1 term a next b))

    測試一下過去寫的那些測試程序,比如生成pi的過程,可以驗證一切正常!

    上面的accumulate過程是遞歸版本,對應的迭代版本也很容易改寫了:
    (define (accumulate-iter combiner term a next b result)
      (
    if (> a b)
          result
          (accumulate
    -iter combiner term (next a) next b (combiner result (term a)))))
    (define (accumulate  combiner null
    -value term a next b)
      (accumulate
    -iter combiner term a next b null-value))

    再看習題1.33,在accumulate的基礎上多增加一個filter的參數(也是一個過程,用于判斷項是否符合要求),在accumulate的基礎上稍微修改下,在每次累積之前進行判斷即可:

    (define (filtered-accumulate combiner null-value term a next b filter)
      (cond ((
    > a b) null-value)
            ((filter a) (combiner (term a) (filtered
    -accumulate combiner null-value term (next a) next b filter))) 
            (
    else (filtered-accumulate combiner null-value term (next a) next b filter))))

    比如,求a到b中的所有素數之和的過程可以寫為(利用以前寫的prime?過程來判斷素數):
    (define (sum-primes a b)
      (filtered
    -accumulate + 0 identity a inc b prime?))

    測試一下:
    > (sum-primes 2 4)
    5
    > (sum-primes 2 7)
    17
    > (sum-primes 2 11)
    28
    > (sum-primes 2 100)
    1060


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 中文字幕在线免费播放| 一级毛片在线免费视频| 182tv免费视视频线路一二三| 伊人久久大香线蕉亚洲五月天| 特a级免费高清黄色片| 免费一区二区视频| 免费国产污网站在线观看不要卡 | 丁香六月婷婷精品免费观看| 国产精品视频免费一区二区三区| 亚洲大码熟女在线观看| 国产精品麻豆免费版| 一级特黄色毛片免费看| 亚洲精品白浆高清久久久久久| 丝袜足液精子免费视频| 亚洲色图国产精品| 91在线品视觉盛宴免费| 亚洲国产成人AV网站| 亚洲夜夜欢A∨一区二区三区| 暖暖免费日本在线中文| 2019亚洲午夜无码天堂| 在线观看91精品国产不卡免费| 免费无码午夜福利片| 国产亚洲精品va在线| 四虎永久在线精品免费观看视频 | 亚洲av无码专区在线观看亚| 亚洲国产成人久久综合区| a级毛片免费在线观看| 亚洲欧洲日产专区| 韩国欧洲一级毛片免费| 亚洲AV午夜成人影院老师机影院| 999久久久免费精品播放| 亚洲日韩在线中文字幕综合 | 亚洲videosbestsex日本| 99免费在线视频| 亚洲欧洲国产视频| 色吊丝最新永久免费观看网站| 中文字字幕在线高清免费电影| 亚洲精品美女视频| 免费黄色福利视频| 国产亚洲综合视频| 久久久久亚洲AV无码专区首JN|