-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum.scm
53 lines (42 loc) · 1.01 KB
/
sum.scm
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
;; Different experiments in Higher order procedures
;; using the sums of certain series...
;;
;; Courtesy: Prof. Gerald Jay Sussman
;;
;; Author : Raghu Ugaré (@raghu_ugare)
;;
;;
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
(define (sum-iter term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (+ result (term a)))))
(iter a 0))
(define (inc n) (+ n 1))
(define (cube x) (* x x x))
(define (square x) (* x x))
(define (sum-cubes a b)
(sum cube a inc b))
(define (same p) p)
(define (sum-integers m n)
(sum identity m inc n))
(define (pi-sum a b)
(define (pi-term x)
(/ 1.0 (* x (+ x 2))))
(define (pi-next x)
(+ x 4))
(sum pi-term a pi-next b))
;; define our own identity procedure! :)
;; NOTE: Need to comment this in Dr.Racket, since it's already defined there.
;;(define identity
;; (lambda (x)
;; x))
(sum-integers 1 10)
(sum-integers 1 100)
(sum-iter identity 1 inc 10)
(* 8 (pi-sum 1 1000))