<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. &nbsp;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'">&nbsp;&nbsp;(define (foo x) (use-common-state-one-way x state))</font></div><div><font class="Apple-style-span" face="'Andale Mono'">&nbsp;&nbsp;(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'">&nbsp;&nbsp;(set! foo (lambda (x) (use-common-state-one-way x state)))</font></div><div><font class="Apple-style-span" face="'Andale Mono'">&nbsp;&nbsp;(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. &nbsp;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>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; scheme/splicing)</div><div><br></div><div>(define-syntax (splicing-let stx)</div><div>&nbsp;&nbsp;(syntax-case stx ()</div><div>&nbsp;&nbsp; &nbsp;[(splicing-let ([x rhs] ...) . b)</div><div>&nbsp;&nbsp; &nbsp; (with-syntax ([(tmp ...) (generate-temporaries #'(x ...))])</div><div>&nbsp;&nbsp; &nbsp; &nbsp; #'(begin</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (define tmp rhs) ...</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (splicing-let-syntax ([x (make-rename-transformer #'tmp)]</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...)</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; . 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"> &nbsp;For list-related administrative tasks:<br> &nbsp;<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>