---
title: "SICP 1.29 答案"
author: "t0uch"
date: "2009-11-11T18:36:38Z"
tags: []
slug: "sicp-1-29"
draft: false
lastmod: "2009-11-11T18:36:38Z"
id: 514836
url: "https://t0uch.iteye.com/blog/514836"
---

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

```scheme
(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)
```