-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.scm
66 lines (53 loc) · 771 Bytes
/
test.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
54
55
56
57
58
59
60
61
62
63
64
65
66
; sum from a to b
(define (sum-int a b)
(if (> a b)
0
(+ a (sum-int (1+ a) b))))
(sum-int 1 10)
; sum from a^2 to b^2
(define (sum-sq a b)
(if (> a b)
0
(+ (square a)
(sum-sq (1+ a) b))))
(sum-sq 1 10)
; pi sum
(define (pi-sum a b)
(if (> a b)
0
(+ (/ 1 (* a (+ a 2)))
(pi-sum (+ a 4) b))))
(pi-sum 1 10)
; common pattern
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term
(next a)
next
b))))
; redefine sum-int
(define (SUMINT a b)
(sum
(lambda (x) x)
a
1+
b))
(SUMINT 1 10)
; redefine sum-sq
(define (SUMSQ a b)
(sum
square
a
1+
b))
(SUMSQ 1 10)
; redefine pi-sum
(define (PISUM a b)
(sum
(lambda (x) (/ 1 (* x (+ x 2))))
a
(lambda (x) (+ x 4))
b))
(PISUM 1 10)