@@ -239,45 +239,38 @@ in what we know so far:
239239
240240@#reader scribble/comment-reader
241241(racketblock
242- ;; Expr REnv Defns -> Answer
242+ ;; Expr Env Defns -> Value { raises 'err }
243243(define (interp-e e r ds)
244244 (match e
245245 ;; ...
246246 [(Lam _ xs e)
247247 (λ ??? '... )]
248248 [(App e es)
249- (match (interp-e e r ds)
250- ['err 'err ]
251- [f
252- (match (interp-e* es r ds)
253- ['err 'err ]
254- [vs
255- (apply f vs)])])]))
249+ (let ((f (interp-e e r ds))
250+ (vs (interp-e* es r ds)))
251+ (f vs))]))
256252)
257253
258254
259255It's not totally clear what parameters the representation of a
260256function should have or what we should in the body of that function.
261257However, the code in the interpretation of an application sheds light
262258on both. First, it's clear a function should potentially take any
263- number of arguments:
259+ number of arguments, which we can accomplish by always taking a single
260+ argument that is a list:
264261
265262@#reader scribble/comment-reader
266263(racketblock
267- ;; Expr REnv Defns -> Answer
264+ ;; Expr Env Defns -> Answer
268265(define (interp-e e r ds)
269266 (match e
270267 ;; ...
271268 [(Lam _ xs e)
272- (λ vs '... )]
269+ (λ (vs) '... )]
273270 [(App e es)
274- (match (interp-e e r ds)
275- ['err 'err ]
276- [f
277- (match (interp-e* es r ds)
278- ['err 'err ]
279- [vs
280- (apply f vs)])])]))
271+ (let ((f (interp-e e r ds))
272+ (vs (interp-e* es r ds)))
273+ (f vs))]))
281274)
282275
283276Second, what should happen when a function is applied? It should
@@ -287,28 +280,24 @@ Translating that to code, we get:
287280
288281@#reader scribble/comment-reader
289282(racketblock
290- ;; Expr REnv Defns -> Answer
283+ ;; Expr Env Defns -> Answer
291284(define (interp-e e r ds)
292285 (match e
293286 ;; ...
294287 [(Lam _ xs e)
295- (λ vs (interp-e e (zip xs vs) ds))]
288+ (λ (vs) (interp-e e (zip xs vs) ds))]
296289 [(App e es)
297- (match (interp-e e r ds)
298- ['err 'err ]
299- [f
300- (match (interp-e* es r ds)
301- ['err 'err ]
302- [vs
303- (apply f vs)])])]))
290+ (let ((f (interp-e e r ds))
291+ (vs (interp-e* es r ds)))
292+ (f vs))]))
304293)
305294
306295And now we have simultaneously arrived at our representation of function values:
307296@#reader scribble/comment-reader
308297(racketblock
309298;; type Value =
310299;; | ....
311- ;; | (Value ... -> Answer )
300+ ;; | ([Listof Value] -> Value { raises 'err } )
312301)
313302
314303and completed the implementation of the interpreter.
@@ -337,46 +326,38 @@ in the (Racket) function:
337326
338327@#reader scribble/comment-reader
339328(racketblock
340- ;; Expr REnv Defns -> Answer
329+ ;; Expr Env Defns -> Answer
341330(define (interp-e e r ds)
342331 (match e
343332 ;; ...
344333 [(Lam _ xs e)
345- (λ vs (interp-e e (append (zip xs vs) r)) ds)]
334+ (λ (vs) (interp-e e (append (zip xs vs) r)) ds)]
346335 [(App e es)
347- (match (interp-e e r ds)
348- ['err 'err ]
349- [f
350- (match (interp-e* es r ds)
351- ['err 'err ]
352- [vs
353- (apply f vs)])])]))
336+ (let ((f (interp-e e r ds))
337+ (vs (interp-e* es r ds)))
338+ (f vs))]))
354339)
355340
356341The last remaining issue is we should do some type and arity-checking:
357342
358343@#reader scribble/comment-reader
359344(racketblock
360- ;; Expr REnv Defns -> Answer
345+ ;; Expr Env Defns -> Answer
361346(define (interp-e e r ds)
362347 (match e
363348 ;; ...
364349 [(Lam _ xs e)
365- (λ vs
350+ (λ (vs)
366351 ; check arity matches
367352 (if (= (length xs) (length vs))
368353 (interp-e e (append (zip xs vs) r) ds)
369- 'err ))]
354+ (raise 'err ) ))]
370355 [(App e es)
371- (match (interp-e e r ds)
372- ['err 'err ]
373- [f
374- (match (interp-e* es r ds)
375- ['err 'err ]
376- [vs
377- (if (procedure? f)
378- (apply f vs)
379- 'err )])])]))
356+ (let ((f (interp-e e r ds))
357+ (vs (interp-e* es r ds)))
358+ (if (procedure? f)
359+ (apply f vs)
360+ (raise 'err )))]))
380361)
381362
382363We have a final issue to deal with. What should we do about
0 commit comments