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


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


    網站導航:
     
    主站蜘蛛池模板: 女同免费毛片在线播放| 一级毛片不卡免费看老司机| 全免费a级毛片免费看| 亚洲精品乱码久久久久久不卡| 亚洲小说图区综合在线| 美女视频黄的全免费视频| 亚洲精品亚洲人成在线麻豆| 一级毛片在线观看免费| 久久久影院亚洲精品| 无码A级毛片免费视频内谢| 亚洲国产成人私人影院| 99re这里有免费视频精品| 亚洲综合婷婷久久| 亚洲免费中文字幕| 亚洲综合av一区二区三区不卡| 精品久久洲久久久久护士免费 | 99热精品在线免费观看| 久久精品国产亚洲网站| 久久久久国产免费| 国产成人精品日本亚洲专| 国产精品免费视频一区| 国产99精品一区二区三区免费 | 无码人妻精品中文字幕免费东京热| 亚洲视频国产精品| 麻豆国产入口在线观看免费 | 美女扒开屁股让男人桶爽免费| 免费又黄又爽又猛的毛片| 中文字幕视频免费在线观看| 久久精品国产亚洲av影院| 最近中文字幕免费mv视频7| 有码人妻在线免费看片| 亚洲AV色香蕉一区二区| 成全视频免费高清| 免费无码又爽又刺激一高潮| 亚洲人成电影网站| 亚洲人成国产精品无码| 国产高清免费视频| 国产精品福利片免费看| 亚洲国产福利精品一区二区| 亚洲免费无码在线| 99无码人妻一区二区三区免费 |