-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsequence.sch
267 lines (237 loc) · 6.44 KB
/
sequence.sch
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
(define (accumulate f value list)
(if (null? list)
value
(accumulate f
(f (car list) value)
(cdr list))))
(define (map p sequence)
(accumulate (lambda (current all)
(cons (p current)all))
'()
(reverse sequence)))
(define (append seq1 seq2)
(accumulate cons
seq2
(reverse seq1)))
(define (length seq)
(accumulate (lambda (x length) (+ length 1))
0
seq))
(define (horner-eval x coefficient-sequence)
(accumulate (lambda (this-coeff higher-terms)
(+ this-coeff
(* x higher-terms)))
0
coefficient-sequence))
(define (count-leaves t)
(accumulate (lambda (number total)
(+ number total))
0
(map (lambda (x)
(if (list? x)
(count-leaves x)
1))
t)))
(define (accumulate-n op init seqs)
(if (null? (car seqs))
'()
(cons (accumulate op init (map car seqs))
(accumulate-n op init (map cdr seqs)))))
(define (accumulate-n f init . seqs)
(define (iter value seqs)
(if (null? (car seqs))
value
(iter (apply f (cons value (map car seqs)))
(map cdr seqs))))
(iter init seqs))
(define (dot-product v w)
(accumulate + 0 (map * v w)))
(define (matrix-*-vector m v)
(map (lambda (row)
(dot-product row v))
m))
(define (transpose mat)
(map reverse
(accumulate-n cons '() mat)))
(define (matrix-*-matrix m n)
(let ((cols (transpose n)))
(map (lambda (row)
(map (lambda (col) (dot-product row col))
cols))
m)))
(define (reverse sequence)
(fold-right (lambda (x y) (cons x y)) '() sequence))
(define (reverse sequence)
(fold-left (lambda (x y) (cons y x) '() sequence)))
(define (flatmap f list)
(fold-right
(lambda (sub-list total)
(fold-right (lambda (x list) (cons x list))
total
sub-list))
'()
(map f list)))
(define (permutations s)
(define (remove x list)
(filter (lambda (y) (not (eq? x y)))
list))
(define (recur s)
(if (null? s) ; empty set?
(list s) ; sequence containing empty set
(flatmap (lambda (x)
(map (lambda (p) (cons x p))
(recur (remove x s))))
s)))
(recur s))
(define (unique-pairs n)
(define (seq x y)
(if (> x y)
'()
(cons x (seq (+ x 1) y))))
(flatmap
(lambda (i)
(map
(lambda (j) (cons i j))
(seq 1 (- i 1))))
(seq 1 n)))
(define (pair-2d n m)
(define (seq x y)
(if (> x y)
'()
(cons x (seq (+ x 1) y))))
(flatmap
(lambda (i)
(map (lambda (j) (cons i j))
(seq 1 m)))
(seq 1 n)))
(define (prime-sum-pairs n)
(define (prime? x)
(define (iter i)
(if (> (square i) x)
#f
(or (= (remainder x i) 0)
(iter (+ i 1)))))
(iter 2))
(filter
(lambda (ij)
(not
(prime?
(+ (car ij)
(cdr ij)))))
(unique-pairs n)))
(define (specific-sum-triples n s)
(define (seq x y)
(if (> x y)
'()
(cons x (seq (+ x 1) y))))
(define (unique-triples n)
(flatmap
(lambda (i)
(flatmap
(lambda (j)
(map
(lambda (k) (list i j k))
(seq 1 j)))
(seq 1 i)))
(seq 1 n)))
(filter
(lambda (triple)
(= (fold + 0 triple) s))
(unique-triples n)))
(define (queens queen-number board-size)
(define (queen-cols k)
;; return value should be:
;; ((q1 q2 ... )
;; (q1 q2 ... ))
;;
;; q1 = (row col)
(if (= k 0)
empty-board
(filter
safe?
(flatmap
(lambda (rest-of-queens)
(map (lambda (row)
(adjoin-position row k rest-of-queens))
(enumerate-interval 1 board-size)))
(queen-cols (- k 1))))))
(define (queen-k k)
;; return value should be:
;; ((q1 q2 ... )
;; (q1 q2 ... ))
;;
;; q1 = (row col)
(if (= k 0)
empty-board
(filter
safe?
(flatmap
(lambda (rest-of-queens)
(map (lambda (new-cell)
(cons new-cell rest-of-queens))
all-cell))
(queen-cols (- k 1))))))
(define (safe? queen-positions)
;; (display queen-positions) (newline)
(define (diff q1 q2)
(cons (- (car q1) (car q2))
(- (cdr q1) (cdr q2))))
(define (slash? diff)
(= (car diff)
(cdr diff)))
(define (backslash? diff)
(= 0 (+ (car diff)
(cdr diff))))
(define (same-row? diff)
(= 0 (car diff)))
(define (same-col? diff)
(= 0 (cdr diff)))
(let ((queen (car queen-positions))
(rest-queen (cdr queen-positions)))
(every (lambda (other-queen)
(let ((q-diff (diff other-queen queen)))
(and (not (slash? q-diff))
(not (backslash? q-diff))
(not (same-row? q-diff))
(not (same-col? q-diff)))))
rest-queen)))
(define (enumerate-interval n m)
(if (> n m)
'()
(cons n (enumerate-interval (+ n 1) m))))
(define (adjoin-position row col rest-of-queens)
(cons (cons row col)
rest-of-queens))
(define empty-board '(()))
(define all-cell
(flatmap
(lambda (row)
(map (lambda (col) (cons row col))
(enumerate-interval 1 board-size)))
(enumerate-interval 1 board-size)))
(queen-cols queen-number))
(define (print-point xy)
(let ((x (car xy))
(y (cdr xy)))
(define (iter i string)
(if (> i 8)
string
(iter (+ i 1)
(string-append string
(if (= i x)
"〇"
"十")))))
(iter 1 "")))
(define (print-board board)
(for-each (lambda (xy-list)
(for-each (lambda (point)
(display (print-point point))
(newline))
xy-list)
(newline))
board))
(define (pipe value . function-list)
(fold-left (lambda (value function)
(function value))
value
function-list))