[plt-scheme] Parameterizing expansion of subexpressions

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Tue Jan 19 15:12:01 EST 2010

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:

(require (for-syntax syntax/parse)
          scheme/stxparam)           ;; <---- syntax-parameters

;; defines 'the-value' as a syntax-parameter
(define-syntax-parameter the-value
   (syntax-rules ()
     [(_) 4]))

(define-syntax (with-value stx)
   (syntax-parse stx
     [(_ val:expr e:expr)
      #'(let ([v val]) ;; only evaluate once
          ;; update 'the-value' for the expansion of 'e'
          (syntax-parameterize ([the-value
                                 (syntax-rules ()
                                   [(_) v])])

(with-value (+ 4 5)
   (sqr (the-value)))
;; => 81


> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.