Macros and expand
Posted: Fri Sep 24, 2004 7:49 pm
I'm trying to understand the proper use of
expand with macros.
So I define the macro "expand-let"
with the following semantics.
(expand-let '(x y z) (x 1) (y 2) (z 3))
"expands" to the equivalent of
(let ((x 1)(y 2)(z 3))
(expand '(x y z) 'x 'y 'z))
returning
(1 2 3)
The following is my implementation of this semantics
(define (keys alist)
(map (fn (pair) (first pair)) alist))
(define (bindings alist)
(map (fn (pair) (cons (first pair) (eval (last pair)))) alist))
(define-macro (expand-let expr)
(let ((ks (keys (rest (args))))
(bs (bindings (rest (args)))))
(eval (expand '(let bs
(apply expand (cons (eval expr) (quote ks))))
'ks 'bs))))
Is the above definition of expand-let reasonable
or is it poorly constructed?
(I have a follow up example/question that builds on nested
macros if this is okay thus far.)
Thanks.
expand with macros.
So I define the macro "expand-let"
with the following semantics.
(expand-let '(x y z) (x 1) (y 2) (z 3))
"expands" to the equivalent of
(let ((x 1)(y 2)(z 3))
(expand '(x y z) 'x 'y 'z))
returning
(1 2 3)
The following is my implementation of this semantics
(define (keys alist)
(map (fn (pair) (first pair)) alist))
(define (bindings alist)
(map (fn (pair) (cons (first pair) (eval (last pair)))) alist))
(define-macro (expand-let expr)
(let ((ks (keys (rest (args))))
(bs (bindings (rest (args)))))
(eval (expand '(let bs
(apply expand (cons (eval expr) (quote ks))))
'ks 'bs))))
Is the above definition of expand-let reasonable
or is it poorly constructed?
(I have a follow up example/question that builds on nested
macros if this is okay thus far.)
Thanks.