-
Notifications
You must be signed in to change notification settings - Fork 0
/
hop.rkt
322 lines (305 loc) · 9.26 KB
/
hop.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#lang racket
;;; File:
;;; hop.rkt
;;; Summary:
;;; A variety of higher-order procedures
;;; Author:
;;; Samuel A. Rebelsky
(provide
(contract-out
[all (-> procedure? list? boolean?)]
[any (-> procedure? list? boolean?)]
[comparator (-> procedure? procedure? procedure?)]
[left-section (-> procedure? any/c procedure?)]
[l-s (-> procedure? any/c procedure?)]
[right-section (-> procedure? any/c procedure?)]
[r-s (-> procedure? any/c procedure?)]
))
(provide o)
(provide section partial Px)
; +---------------------+--------------------------------------------
; | Exported procedures |
; +---------------------+
;;; Procedure:
;;; all
;;; Parameters:
;;; pred?, a unary predicate
;;; lst, a list
;;; Purpose:
;;; Determine if pred? holds for all the values in lst.
;;; Produces:
;;; ok?, a Boolean
;;; Preconditions:
;;; [Standard]
;;; Postconditions:
;;; If there is an i such that (pred? (list-ref lst i))
;;; fails to hold, then ok? is false.
;;; Otherwise, ok? is true.
(define all
(lambda (pred? lst)
(or (null? lst)
(and (pred? (car lst))
(all pred? (cdr lst))))))
;;; Package:
;;; loudhum/hop
;;; Procedure:
;;; any
;;; Parameters:
;;; pred?, a unary predicate
;;; lst, a list
;;; Purpose:
;;; Determines if pred? holds for any of the values in lst
;;; Produces:
;;; ok?, a Boolean
;;; Preconditions:
;;; [No additional]
;;; Postconditions:
;;; If there is an i s.t. (pred? (list-ref lst i)) holds, then
;;; ok? is true.
;;; If for all i, (pred? (list-ref list i)) does not hold, then
;;; ok? is false.
(define any
(lambda (pred? lst)
(and (not (null? lst))
(or (pred? (car lst))
(any pred? (cdr lst))))))
;;; Package:
;;; loudhum/hop
;;; Procedure:
;;; comparator
;;; Parameters:
;;; compare?, a binary comparator
;;; extract, a unary procedure
;;; Purpose:
;;; Creates a comparator that takes two values, applies extract
;;; to each, and then compares the results of both.
;;; Produces:
;;; comp?, a binary comparator
;;; Preconditions:
;;; compare? can be applied to the results of extract.
;;; Postconditions:
;;; (comp? v1 v2) = (compare? (extract v1) (extract v2))
(define comparator
(lambda (compare? extract)
(lambda (v1 v2)
(compare? (extract v1) (extract v2)))))
;;; Package:
;;; loudhum/hop
;;; Procedure:
;;; left-section
;;; Parameters:
;;; proc2, a two-parameter procedure
;;; left, a value
;;; Purpose:
;;; Creates a one-parameter procedure by filling in the first parameter
;;; of proc2.
;;; Produces:
;;; proc1, a one-parameter procedure
;;; Preconditions:
;;; left is a valid first parameter for proc2.
;;; Postconditions:
;;; (proc1 right) = (proc2 left right)
(define left-section
(lambda (proc2 left)
(lambda (right) (proc2 left right))))
;;; Package:
;;; loudhum/hop
;;; Procedure:
;;; l-s
;;; Parameters:
;;; proc2, a two-parameter procedure
;;; left, a value
;;; Purpose:
;;; Creates a one-parameter procedure by filling in the first parameter
;;; of proc2.
;;; Produces:
;;; proc1, a one-parameter procedure
;;; Preconditions:
;;; left is a valid first parameter for proc2.
;;; Postconditions:
;;; (proc1 right) = (proc2 left right)
(define l-s left-section)
;;; Package:
;;; loudhum/hop
;;; Procedure:
;;; right-section
;;; Parameters:
;;; proc2, a two-parameter procedure
;;; right, a value
;;; Purpose:
;;; Creates a one-parameter procedure by filling in the second parameter
;;; of proc2.
;;; Produces:
;;; proc1, a one-parameter procedure
;;; Preconditions:
;;; left is a valid first parameter for proc2.
;;; Postconditions:
;;; (proc1 left) = (proc2 left right)
(define right-section
(lambda (proc2 right)
(lambda (left) (proc2 left right))))
;;; Package:
;;; loudhum/hop
;;; Procedure:
;;; r-s
;;; Parameters:
;;; proc2, a two-parameter procedure
;;; right, a value
;;; Purpose:
;;; Creates a one-parameter procedure by filling in the second parameter
;;; of proc2.
;;; Produces:
;;; proc1, a one-parameter procedure
;;; Preconditions:
;;; left is a valid first parameter for proc2.
;;; Postconditions:
;;; (proc1 left) = (proc2 left right)
(define r-s right-section)
; +-----------------+------------------------------------------------
; | Exported macros |
; +-----------------+
;;; Package:
;;; loudhum/hop
;;; Macro:
;;; partial
;;; Parameters:
;;; exp, an expression containing zero or more parameter symbols (<>)
;;; Purpose:
;;; Create a new procedure whose body is exp, with all <>'s replaced
;;; by parameters.
;;; Produces:
;;; newproc, a procedure
(define-syntax partial
(letrec ([kernel
(lambda (exp)
(cond
[(null? exp)
(list null null)]
[(equal? exp '<>)
(let ([param (gensym "param-")])
(list (list param) param))]
[(not (pair? exp))
(list null exp)]
[else
(let ([fa-rest (kernel (cdr exp))])
(cond
[(pair? (car exp))
(let [(fa1 (kernel (car exp)))]
(list (append (car fa1) (car fa-rest))
(append (cadr fa1) (cadr fa-rest))))]
[(or (eq? '<> (car exp))
(eq? '_ (car exp)))
(let [(param (gensym "param-"))]
(list (cons param (car fa-rest))
(cons param (cadr fa-rest))))]
[else
(list (car fa-rest)
(cons (car exp) (cadr fa-rest)))]))]))])
(lambda (stx)
(let ([info (syntax->datum stx)])
(cond
[(symbol? info)
(datum->syntax stx '(quote <macro:partial>))]
[(null? (cdr info))
(error "partial: Requires an expression")]
[(and (not (null? (cddr info))) (not (equal? (caddr info) 'code)))
(write (caddr info)) (newline)
(error "partial: Requires a single expression" info)]
[else
(let [(code (cons 'lambda (kernel (cadr info))))]
(if (not (null? (cddr info)))
(datum->syntax stx (list 'quote code))
(datum->syntax stx code)))])))))
(define-syntax (Px stx)
(let ([info (syntax->datum stx)])
(cond
[(symbol? info)
(datum->syntax stx '(quote <macro:Px>))]
[else
(datum->syntax stx (cons 'partial (cdr info)))])))
;; Package:
;;; loudhum/hop
;;; Procedure:
;;; section
;;; Parameters:
;;; proc, a procedure
;;; param-1, a value or the parameter symbol (<>)
;;; param-2, a value or the parameter symbol (<>)
;;; ...
;;; param-n, a value or the parameter symbol (<>)
;;; Purpose:
;;; Create a new procedure
;;; Produces:
;;; newproc, a procedure
;;; Preconditions:
;;; proc accepts n parameters
;;; Postconditions:
;;; newproc accepts one parameter for each param that is the
;;; parameter symbol.
;;; (newproc val1 ... valk) = (proc ...)
;;; where the ith parameter to proc is either param-i, if param-i
;;; is not <>, and the next element of val1...valk otherwise.
(define-syntax section
(lambda (stx)
(let ([info (syntax->datum stx)])
(cond
[(symbol? info)
(datum->syntax stx '(quote <macro:section>))]
[(null? (cdr info))
(error "section: Requires a procedure")]
[else
(let ([sec (car info)]
[proc (cadr info)]
[params (cddr info)])
(let kernel ([params params]
[formals null]
[actuals null])
(cond
[(null? params)
(let ([code (list 'lambda (reverse formals)
(cons proc (reverse actuals)))])
; (write code) (newline) ; CHECKING
(datum->syntax stx code))]
[(eq? (car params) '<>)
(let ([formal (gensym)])
(kernel (cdr params)
(cons formal formals)
(cons formal actuals)))]
[else
(kernel (cdr params)
formals
(cons (car params) actuals))])))]))))
;;; Package:
;;; loudhum/hop
;;; Macro:
;;; o
;;; Parameters:
;;; fun1, a unary function
;;; fun2, a unary function
;;; ...
;;; funn, a unary function
;;; Purpose:
;;; Compose fun1 ... funn
;;; Produces:
;;; fun, a function
;;; Preconditions:
;;; Each function can be applied to the results of the subsequent
;;; function.
;;; Postconditions:
;;; (fun x) = (fun1 (fun2 (.... (funn x)...)))
(define-syntax o
(lambda (stx)
(let ([info (syntax->datum stx)])
(cond
[(symbol? info)
(datum->syntax stx '(quote <macro:o>))]
[else
(let ([param (gensym "param-")])
(let kernel ([remaining (reverse (cdr info))]
[body param])
(if (null? remaining)
(let ([code `(lambda (,param) ,body)])
; (display code) (newline) ; experiment
(datum->syntax stx code))
(kernel (cdr remaining)
(list (car remaining) body)))))]))))