ITeye博客

SICP 1.29 答案

t0uch · 2009-11-11 18:36 · SICP · Markdown

感觉做这个题目是要形成一种思想,这个算法很牛,比书题目上面的那个算法精确多了。

(define (simpson-intergral f a b n)
  (define (inc-a x)
    (+ x 1))
  (define (h)
    (/ (- b a) n))
  (define (y x)
    (+ a (* x (h))))
  (define (num x) 
    (cond ((or (= x 0) (= x n)) 1)
          ((even? x) 2)
          (else 4)))
  (define (term x)
    (* (num x) (f (y x))))
  (* (/ (h) 3.0) (sum term 0 inc-a n)))

(simpson-intergral cube 0 1 100)

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。