[racket-dev] error using local-expand

From: Sam Tobin-Hochstadt (samth at ccs.neu.edu)
Date: Fri Dec 9 16:55:29 EST 2011

On Fri, Dec 9, 2011 at 4:53 PM, Stephen Chang <stchang at ccs.neu.edu> wrote:
> Say I have the following program:
>
>
> #lang racket
>
> (define-for-syntax (loc-expand stx) (local-expand stx 'expression '()))
>
> (define-syntax (my-begin stx)
>  (syntax-case stx ()
>    [(_ e ...)
>     (with-syntax ([(x ...) (map loc-expand (syntax->list #'(e ...)))])
>       #'(begin x ...))]))
>
> (define-syntax (my-lambda1 stx)
>  (syntax-case stx ()
>    [(_ args body ...)
>     #'(lambda args (my-begin body ...))]))

`my-lambda1' does the local expansion after the `lambda' form
introduces the bindings from `args' into the environment.

>
> (define-syntax (my-lambda2 stx)
>  (syntax-case stx ()
>    [(_ args body ...)
>     (with-syntax ([(x ...) (map loc-expand (syntax->list #'(body ...)))])
>     #'(lambda args (begin x ...)))]))

`my-lambda2' does the local expansion before the `lambda' form
introduces the bindings from `args' into the environment, meaning that
there's no binding yet for any of the arguments.

>
> An expression (my-lambda1 (y) (+ y 1)) works fine but (my-lambda2 (y)
> (+ y 1)) produces the error:
>
>    expand: unbound identifier in module in: y
>
>
> I can't figure out why this is the case. Anyone know?
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/dev



-- 
sam th
samth at ccs.neu.edu



Posted on the dev mailing list.