22 lines
471 B
Scheme
22 lines
471 B
Scheme
#lang sicp
|
|
; (define (cube x) (* x x x))
|
|
|
|
(define (sum term a next b)
|
|
(if (> a b)
|
|
0
|
|
(+ (term a)
|
|
(sum term (next a) next b))))
|
|
|
|
(define (inc x) (+ 1 x))
|
|
|
|
(define (simpson f a b n)
|
|
(define h (/ (- b a) n))
|
|
(define (add-h x) (+ x h))
|
|
(define (term i)
|
|
(cond ((= i 0) (f a))
|
|
((= i n) (f b))
|
|
((even? i) (* 2 (f (+ a (* i h)))))
|
|
(else (* 4 (f (+ a (* i h)))))))
|
|
(* h
|
|
(/ 1.0 3)
|
|
(sum term 0 inc n)))
|