<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On Mar 17, 2009, at 3:32 PM, Jeff de Vries wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">I'm trying to create a macro that creates a set of global function definitions where those functions share some common state. Basically, the intent is something like the following:<div><br></div><div><font class="Apple-style-span" face="'Andale Mono'">;;; doesn't work, creates local definitions instead of global ones</font></div><div><font class="Apple-style-span" face="'Andale Mono'">(let ((state (create-common-state)))</font></div><div><font class="Apple-style-span" face="'Andale Mono'"> (define (foo x) (use-common-state-one-way x state))</font></div><div><font class="Apple-style-span" face="'Andale Mono'"> (define (bar x) (use-common-state-another-way x state)))</font></div><div><br></div><div>I ended up making a macro that generates the following:</div><div><br></div><div><font class="Apple-style-span" face="'Andale Mono'">(define foo null)</font></div><div><font class="Apple-style-span" face="'Andale Mono'">(define bar null)</font></div><div><div><font class="Apple-style-span" face="'Andale Mono'">(let ((state (create-common-state)))</font></div><div><font class="Apple-style-span" face="'Andale Mono'"> (set! foo (lambda (x) (use-common-state-one-way x state)))</font></div><div><font class="Apple-style-span" face="'Andale Mono'"> (set! bar (lambda (x) (use-common-state-another-way x state))))</font></div><div><br></div><div>That works, but it seems a little clunky. Is there a better way?</div></div></div></blockquote><div><br></div><div>Essentially, you want a splicing version of 'let'. It's a "splicing" binding form in the sense that the definitions inside the local binding should be visible outside of it. The 'scheme/splicing' module has several splicing forms, but no 'splicing-let'. Here's one way to write it:</div><div><br></div><div><div>#lang scheme</div><div>(require (for-syntax scheme/base)</div><div> scheme/splicing)</div><div><br></div><div>(define-syntax (splicing-let stx)</div><div> (syntax-case stx ()</div><div> [(splicing-let ([x rhs] ...) . b)</div><div> (with-syntax ([(tmp ...) (generate-temporaries #'(x ...))])</div><div> #'(begin</div><div> (define tmp rhs) ...</div><div> (splicing-let-syntax ([x (make-rename-transformer #'tmp)]</div><div> ...)</div><div> . b)))]))</div><div><br></div><div>Perhaps this should go into 'scheme/splicing'?</div><div><br></div></div><div>Ryan</div><div><br></div><blockquote type="cite"><span class="Apple-style-span" style="-webkit-text-stroke-width: -1; ">_________________________________________________</span></blockquote><blockquote type="cite"> For list-related administrative tasks:<br> <a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a><br></blockquote></div><br></body></html>