标签:ide def 3.1 填充 ext fine 2.0 next define
(define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b)))) (sum-integers 1 10) (define (sum-cubes a b) (define (cube x) (* x x x)) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b)))) (sum-cubes 1 10) (define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b)))) (* 8 (pi-sum 1 1000000))
对过程的抽象:
(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b))))
填充:
(define (inc n) (+ n 1)) (define (cube n) (* n n n)) (define (sum-cubes a b) (sum cube a inc b)) (sum-cubes 1 10)
(define (inc n) (+ n 1)) (define (identity x) x) (define (sum-integers a b) (sum identity a inc b)) (sum-integers 1 10)
(define (pi-next n) (+ n 4)) (define (pi-term x) (/ 1.0 (* x (+ x 2)))) (define (pi-sum a b) (sum pi-term a pi-next b)) (* 8 (pi-sum 1 10000))
拓展:
(define (integral f a b dx) ;积分 (define (add-dx x) (+ x dx)) (* (sum f (+ a (/ dx 2.0)) add-dx b) dx)) (define (cube x) (* x x x)) (integral cube 0 1 0.0001)
标签:ide def 3.1 填充 ext fine 2.0 next define
原文地址:http://www.cnblogs.com/R4mble/p/7892680.html