<2009年3月>
          22232425262728
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          留言簿(4)

          隨筆分類(157)

          搜索

          •  

          最新評論

          閱讀排行榜

          Exercise 1.11.  A function f is defined by the rule that f(n) = n if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n> 3. Write a procedure that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative process.

          recursive:

          (define (fn n)
            (cond ((>= n 3) (+ (+ (fn (- n 1)) (* 2 (fn (- n 2)))) (* 3 (fn (- n 3)))))
                  ((< n 3) n)
             ))


           iterative:

          (define (re n)
            (if (< n 3)
                n
                (iter 2 1 0 n)
                ))
          (define (iter a b c n)
          (if(= n 3)
             (ca a b c)
             (iter (ca a b c) a b (- n 1))
             )
          )
          (define (ca a b c)
            (+ a (* 2 b) (* 3 c) )
          )

          posted on 2009-03-10 08:51 lzj520 閱讀(216) 評論(0)  編輯  收藏 所屬分類: 個人學習日記sicp
          主站蜘蛛池模板: 梁河县| 澄城县| 积石山| 德安县| 贡山| 墨竹工卡县| 伽师县| 日照市| 饶阳县| 蓝山县| 西吉县| 香河县| 方正县| 德州市| 钟山县| 金堂县| 堆龙德庆县| 天津市| 手游| 静宁县| 威远县| 漯河市| 安吉县| 林口县| 海淀区| 南木林县| 和硕县| 蕲春县| 阿克陶县| 红安县| 临西县| 商洛市| 且末县| 桃源县| 彩票| 北票市| 平江县| 深泽县| 鄂托克前旗| 巧家县| 泾源县|