-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab3.rkt
88 lines (67 loc) · 1.75 KB
/
lab3.rkt
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#lang racket
(define (generic-sum a b term next)
(if (> a b)
0
(+ (term a) (generic-sum (next a) b term next))
)
)
(define (succ a)
(+ a 1)
)
(define (id a)
a
)
(generic-sum 1 3 id succ)
(define (accumulate start end term next null-value op)
(if (> start end)
null-value
(op (term start) (accumulate (next start) end term next null-value op))
)
)
(accumulate 1 3 id succ 1 *)
(define (apply-twice f arg)
(define (apply-twice-helper iter arg)
(if (< iter 2)
(apply-twice-helper (+ iter 1) (f arg))
arg
)
)
(apply-twice-helper 0 arg)
)
(apply-twice (lambda (x) (* x x)) 5)
(define (apply-n number f arg)
(if (= number 1)
(f arg)
(apply-n (- number 1) f (f arg))
)
)
(apply-n 2 (lambda (x) (* x x)) 5)
(define (factorial number)
(accumulate 1 number id (lambda (x) (+ x 1)) 1 *)
)
(factorial 5)
(define (double-factorial number)
(accumulate 1 number id (lambda (x) (+ x 2)) 1 *)
)
(double-factorial 5)
(define (power base exp)
(accumulate 1 exp (lambda (x) base) (lambda (x) (+ x 1)) 1 *)
)
(power 2 6)
(define (filter-accumulate start end term next null-value op filter)
(if (> start end)
null-value
(if (= (filter start) 1)
(op (term start) (filter-accumulate (next start) end term next null-value op filter))
(filter-accumulate (next start) end term next null-value op filter)
)
)
)
(define (count p? a b)
(filter-accumulate a b (lambda (x) 1) (lambda (x) (+ x 1)) 0 + p?)
)
(count (lambda (x) (if (= (remainder x 2) 1) 1 0) ) 2 8)
(define (sum-divisors number)
(filter-accumulate 1 (/ number 2) (lambda (x) x) (lambda (x) (+ x 1)) 0 + (lambda (x) (if (= (remainder number x) 0) 1 0)))
)
(sum-divisors 6)