-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.scm
91 lines (76 loc) · 2.06 KB
/
util.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
; utility method the SICP execrises.
(define (expmod base exp m)
;(format #t "base: ~A, exp: ~A, m: ~A ~%" base exp m)
(cond ((= exp 0) 1)
((even? exp)
(remainder (square (expmod base (/ exp 2) m)) m))
(else
(remainder (* base (expmod base (- exp 1) m)) m)
)
)
)
(define (fermat-test n)
;(format #t "fermat-test ~%")
(define (try-it a)
(= (expmod a n n) a))
(try-it (+ 1 (random (- n 1))))
)
(define (fast-prime? n times)
;(format #t "fast-prime? ~%")
(cond ((= times 0) true)
((fermat-test n) (fast-prime? n (- times 1)))
(else false))
)
(define (gcd a b)
(if (= b 0)
a
(gcd b (remainder a b)))
)
(define (average a b)
(/ (+ a b)
2)
)
(define (expt b n)
(define (expt-iter base x product)
(if (= 0 x)
product
(if (even? x)
(expt-iter (square base) (/ x 2) product)
(expt-iter base (- x 1) (* product base)))))
(expt-iter b n 1)
)
(define nil '())
(define (enumerate-interval low high)
(if (> low high)
nil
(cons low (enumerate-interval (+ low 1) high)))
)
(define (filter predicate sequence)
(cond ((null? sequence) nil)
((predicate (car sequence))
(cons (car sequence) (filter predicate (cdr sequence))))
(else (filter predicate (cdr sequence))))
)
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence))))
)
(define (enumerate-tree tree)
(cond ((null? tree) nil)
((not (pair? tree)) (list tree))
(else (append (enumerate-tree (car tree))
(enumerate-tree (cdr tree)))))
)
(define fold-right accumulate)
(define (fold-left op initial seq)
(define (iter result rest)
(if (null? rest)
result
(iter (op result (car rest)) (cdr rest))))
(iter initial seq)
)
(define (flatmap proc seq)
(accumulate append nil (map proc seq))
)