[plt-scheme] Parameterizing expansion of subexpressions

From: Neil Toronto (ntoronto at cs.byu.edu)
Date: Tue Jan 19 16:30:15 EST 2010

Ryan Culpepper wrote:
> Neil Toronto wrote:
>> What's the best way to parameterize the expansion of subexpressions? 
>> I've tried to make syntax parameters work, but I need the parameter 
>> values at expansion time, and it doesn't seem to be made for that.
> 
> The solution is ... "syntax-parameters", which is something different 
> from using parameters at compile time. Here's the code:

I did try syntax-parameters proper, but I put the syntax-parameterize 
*outside* the returned syntax within with-value. Heh.

Anyway, you and McCarthy-sensei have set me straight. I wanted something 
like this:


(require (for-syntax syntax/parse)
          scheme/stxparam-exptime
          scheme/stxparam)

(define-syntax-parameter the-value #'4)

(define-syntax (with-value stx)
   (syntax-parse stx
     [(_ val:expr e:expr)
      #'(syntax-parameterize ([the-value #'val])
          e)]))

(define-syntax (observe stx)
   (printf "The value is: ~S~n"
           (syntax-parameter-value #'the-value))
   #'(void))

(observe)  ; prints #'4

(with-value (+ 4 5)  ; prints #'(+ 4 5)
   (observe))


Thanks!
Neil T



Posted on the users mailing list.