aboutsummaryrefslogtreecommitdiff
path: root/SICP/ch1_3.scm
blob: 76fadb74ca2e2a706a676df0e26b5b3e98e6eb7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(define (cube x) (* x x x))

;; Sum from a to b.

(define (sum term a next b)
  (if (> a b )
    0
    (+ (term a)
       (sum term (next a) next b))))

(define (inc n) (+ n 1))
(define (sum-cubes a b)
  (sum cube a inc b))

(sum-cubes 1 10)

(define (identity x) x)
(define (sum-integers a b)
  (sum identity a inc b))
(sum-integers 1 10)

(define (pi-sum a b)
  (define (pi-term x)
    (/ 1.0 (* x (+ x 2))))
  (define (pi-next x)
    (+ x 4))
(* 8 (sum pi-term a pi-next b)))
(pi-sum 1 1000)

(define (intergral f a b dx)
  (define (add-dx x) (+ x dx))
  (* (sum f (+ a (/ dx 2.0)) add-dx b) dx))
(intergral cube 0 1 0.0001)