some solutions for MIT Press's "structure and interpretation of computer programs", I guess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
537 B

(define (cube x)
(* x x x))
(define (inc x)
(+ x 1))
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
(define (integral f a b dx)
(define (add-dx x)
(+ x dx))
(* (sum f (+ a (/ dx 2.0)) add-dx b)
dx))
(define (simpson f a b n)
(define h (/ (- b a) n))
(define (yk k) (f (+ a (* h k))))
(define (simpson-term k)
(* (cond ((or (= k 0) (= k n)) 1)
((odd? k) 4)
(else 2))
(yk k)))
(* (/ h 3.0) (sum simpson-term 0 inc n)))