[plt-scheme] Parameterizing expansion of subexpressions

From: Carl Eastlund (carl.eastlund at gmail.com)
Date: Tue Jan 19 16:35:52 EST 2010

On Tue, Jan 19, 2010 at 4:30 PM, Neil Toronto <ntoronto at cs.byu.edu> wrote:
> 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))

Why are you saving an expansion-time expression, instead of a run-time
value?  You could easily write a macro that saved a value in a
parameter each time you ran with-value; with an extra lambda-wrapping
at with-value and application at observe, you could have the
expression be re-executed at each observe if that's your intention.  I
don't see why you want the expression itself to be lifted out and
re-expanded at each call site.  Am I missing something obvious?

--Carl


Posted on the users mailing list.