[plt-scheme] simple macro

From: Eli Barzilay (eli at barzilay.org)
Date: Wed Mar 2 17:56:33 EST 2005

On Mar  2, David J. Neu wrote:
> 
>  (define-syntax init
>   (syntax-rules ()
>     ((_ sym def) (cond ((assq (quote sym) args) => cadr)
>                        (else def)))))

Think lexical scope -- if you look at this code only, then `args' is
undefined.  It's the same error that would happen if you would
implement this as a function instead of a macro.


>  (define f
>    (lambda (x . args)
>      (let  ((y (init y 2)))
>        (printf "x=~a y=~a~n" x y))))
> 
> (f 1 '(y 3))

Here `arg' is bound, but the syntax definition is not in this scope.

This will work:

  (define-syntax (init stx)
    (syntax-case stx ()
      [(_ sym def)
       (with-syntax ([args (datum->syntax-object stx 'args stx)])
         #'(cond [(assq (quote sym) args) => cadr]
                 (else def)))]))

but the fact that it's not as trivial as your version should raise a
good question -- do you *want* this thing?

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!



Posted on the users mailing list.