blob: 3af4acf218ed6cdd948b707e60cd34bc8a5f0ced (
plain) (
blame)
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
|
;;; APL interpreter project apl.scm
(define (----YOU-FILL-THIS-IN----) '()) ; just so file will load
;;; Step 1: convert scalar procedures to array procedures
(define (single x) ; reduce order of single-element list
(cond ((not (pair? x)) x)
((null? (cdr x)) (single (car x)))
(else x)))
(define (apl-dyadic op) ; turn dyadic scalar function into APL array fn
(define (newop x y)
(let ((xx (single x))
(yy (single y)))
(cond ((and (number? xx) (number? yy))
(----YOU-FILL-THIS-IN----))
((number? xx) (map (----YOU-FILL-THIS-IN----) yy))
((number? yy) (map (----YOU-FILL-THIS-IN----) xx))
(else (map newop xx yy)))))
newop)
;;; Step 2: APL primitive operations
(define (iota n) ; monadic iota
(define (iter x n)
(if (> x n)
'()
(cons x (iter (1+ x) n))))
(iter 1 (single n)))
(define (reshape shape l) ; dyadic rho
(define (circular l)
(define (c1 pair)
(if (null? (cdr pair))
(set-cdr! pair l)
(c1 (cdr pair))))
(c1 l)
l)
(define token
(let ((source (circular (ravel l))))
(lambda ()
(let ((out (car source)))
(set! source (cdr source))
out))))
(define (string n shape)
(if (= n 0)
'()
(let ((top (re1 shape)))
(cons top (string (-1+ n) shape)))))
(define (re1 shape)
(if (null? shape)
(token)
(string (car shape) (cdr shape))))
(re1 shape))
(define (cat a b) ; dyadic comma
(define (depth l)
(if (not (pair? l))
0
(1+ (depth (car l)))))
(define (max x y)
(if (> x y) x y))
(define (shapeup l dims)
(if (= dims (depth l))
l
(shapeup (cons l '()) dims)))
(let ((dim (max (depth a) (depth b))))
(append (shapeup a dim) (shapeup b dim))))
(define (ravel l) ; monadic comma
(define (r1 this rest)
(cond ((null? this) (ravel rest))
((not (pair? this)) (cons this (ravel rest)))
(else (r1 (car this) (cons (cdr this) rest)))))
(cond ((null? l) '())
((not (pair? l)) (cons l '()))
(else (r1 (car l) (cdr l)))))
(define (abs x) ; monadic bar
(if (< x 0) (- x) x))
(define (rem x y) ; dyadic bar
(remainder y x))
;;; data abstraction for APL operators
(define (tri-op name nil mon dy)
(list name nil mon dy))
(define (apl-operator? op)
(assq op apl-operators))
(define (niladic op)
(cadr (assq op apl-operators)))
(define (monadic op)
(caddr (assq op apl-operators)))
(define (dyadic op)
(cadddr (assq op apl-operators)))
(define (make-niladic name body)
(set! apl-operators (cons (tri-op name body error error) apl-operators)))
(define (make-monadic name body)
(set! apl-operators (cons (tri-op name error body error) apl-operators)))
(define (make-dyadic name body)
(set! apl-operators (cons (tri-op name error error body) apl-operators)))
(define (make-op op mon dy) ; abbreviation for primitives, never niladic
(tri-op op error mon dy))
(define (make-scalar-op op mon dy)
(make-op op (apl-monadic mon) (apl-dyadic dy)))
;;; Table of operations
(define (apl-pred2 op) ; turn Lisp predicate (t/f) into APL (0/1)
(lambda (x y)
(if (op x y) 1 0)))
(define apl-operators
(list (make-scalar-op '+ (lambda (x) x) +)
(make-scalar-op '- - -)
(make-scalar-op '*
(lambda (x) (cond ((< x 0) -1)
((= x 0) 0)
(else 1)))
*)
(make-scalar-op '% / /)
(make-scalar-op 'bar abs rem)
(make-scalar-op '= error (apl-pred2 =))
(make-scalar-op '< error (apl-pred2 <))
(make-scalar-op '> error (apl-pred2 >))
(make-op '/ error compress)
(make-op 'iota iota error)
(make-op 'rho shape reshape)
(make-op 'comma ravel cat)))
;;; APL higher-order operations
(define (reduce op l) ; higher-order /
(if (null? (cdr l))
(car l)
((dyadic op) (car l) (reduce op (cdr l)))))
(define (apl-hof? op)
(assq op apl-hofs))
(define (hof op)
(cdr (assq op apl-hofs)))
(define apl-hofs (list (cons '/ reduce)))
;;; Step 3: Syntax conversion, infix to prefix
(define (get-operand l)
(cond ((null? l) '())
((list? (car l))
(get-dyad (get-operand (car l)) (cdr l)))
((number? (car l)) (get-vector l))
((apl-operator? (car l))
(cond ((null? (cdr l)) (error "dangling operator" (car l)))
((apl-hof? (cadr l))
(list (hof (cadr l)) (car l) (get-operand (cddr l))))
(else
(list (monadic (car l)) (get-operand (cdr l))))))
(else (get-dyad (car l) (cdr l)))))
(define (get-dyad left l)
(cond ((null? l) left)
((apl-operator? (car l))
(----YOU PUT SOMETHING HERE----))
(else
(error "operand where operator expected" (car l)))))
(define (get-vector l)
(define (gv vect l)
(cond ((null? l) vect)
((number? (car l))
(----YOU PUT SOMETHING HERE----))
(else (get-dyad vect l))))
(gv '() l))
;;; mini-evaluator
(define (apl-loop)
(define (maybe-display val)
(if (eq? val 'no-value) '() (display val)))
(newline)
(display "APL> ")
(maybe-display (apl-eval (get-operand (read))))
(apl-loop))
(define (apl-eval l)
(cond ((not (pair? l)) l)
((procedure? (car l)) (apply (car l) (map apl-eval (cdr l))))
(else l)))
(define *keyboard-interrupt-handler* reset)
;;; Step 11: Procedure definition
(define (convert-syntax l)
(cond ((not (pair? l)) l)
((eq? (car l) 'del) (proc-definition (cdr l)) 'no-name)
(else (get-operand l))))
(define (proc-definition l)
(cond ((null? (cdr l))
(make-niladic (car l) (make-procedure '() '() '() (proc-body))))
((eq? (cadr l) 'gets)
(proc-result (car l) (cddr l)))
(else (----YOU-FILL-THIS-IN----))))
(define (proc-result outvar l)
(define (count-to-locals l)
(cond ((null? l) 0)
((eq? (car l) ':) 0)
(else (1+ (count-to-locals (cdr l))))))
(define (locals l)
(cond ((null? l) '())
((eq? (car l) ':)
(cons (cadr l) (locals (cddr l))))
(else (error "bad format in locals" l))))
(let ((adic (count-to-locals l)))
(cond ((= adic 1)
(make-niladic (car l)
(make-procedure outvar
'()
(locals (cdr l))
(proc-body))))
((= adic 2)
(make-monadic (car l)
(make-procedure outvar
(list (cadr l))
(locals (cddr l))
(proc-body))))
((= adic 3)
(----YOU-FILL-THIS-IN----))
(else (error "too many args in function definition" l)))))
(define (proc-body)
(define (proc-body-loop lineno)
(display "[")
(display lineno)
(display "] ")
(let ((next (read)))
(if (eq? next 'del)
'()
(cons next (proc-body-loop (1+ lineno))))))
(proc-body-loop 1))
|