--- Begin Message ---
|
Subject: |
Re: define-values implementation wanted for SISC / Chez Scheme / syntax-case |
Abdulaziz Ghuloum wrote:
You're in the right track in using the portable-syntax-case modules.
Here is how to do it (tested with chez).
(define-syntax define-values
(lambda (ctx)
(syntax-case ctx ()
[(_ (x* ...) e)
(with-syntax ([(y* ...) (generate-temporaries #'(x* ...))])
#'(module (x* ...)
(define x* (void)) ...
(call-with-values (lambda () e)
(lambda (y* ...)
(set! x* y*) ...))))])))
Thank you very much! What I couldn't figure out was, if all the
definitions were done first, how to get the multiple values to the
set!'s without evaluating the multiple value expression more than once.
Your solution is to push the expression down to the set!'s, which
makes perfect sense.
Here's an implementation in syntax-rules:
(define-syntax define-values
(syntax-rules ()
((define-values "generate-lambda" () (value ...) (set ...))
(lambda (value ...)
set ...))
((define-values "generate-lambda"
(variable1 variable ...) (value ...) (set ...))
(define-values "generate-lambda"
(variable ...)
(value ... temporary)
(set ... (set! variable1 temporary))))
((define-values (variable ...) expression)
(module (variable ...)
(define variable (void)) ...
(call-with-values (lambda () expression)
(define-values "generate-lambda" (variable ...) () ()))))))
Here each recursive expansion of "generate-lambda" generates a temporary
for us, as "temporary" is free.
However I think your implementation is clearer.
I dedicate my code here to the public domain, in case it is useful to
anyone else.
Thank you!
Andrew Wilcox
email: scheme[at]andrewwilcox.name
--- End Message ---