hello,<div>I want to make a portable module system using lexical scoping (1).</div><div><br></div><div>By using a letrec form, one can write</div><div><br></div><div>(define my-module</div><div>  (lambda ()</div><div>    (letrec ((private-fun (lambda () ..))</div>
<div>                 (public-fun (lambda () ..)))</div><div>      (list 'public-fun public-fun))))</div><div><br></div><div>I use the let () form wich I believe is equivalent :</div><div><br></div><div>(define my-module</div>
<div>  (lambda ()</div><div>    (let ()</div><div>      (define private-fun</div><div>        (lambda () ..))</div><div>      (define public-fun (lambda () ..))</div><div>      (list 'public-fun public-fun))))</div><div>
<br></div><div>But I have an annoying limitation with letrec. </div><div>For letrec, each init expression must evaluate without referencing or assigning the value of any of the vars.</div><div><br></div><div>That is a problem when I want this code to be encapsulated in a module :</div>
<div><br></div><div>(define private-fun (lambda (x) ...)</div><div>(define public-var (private-fun 'a-value))</div><div><br></div><div>by putting these expression into a let () form violate the scheme rules (2)</div><div>
The letrec* macro does exactly what I want, unfortunately it is not available in a r5rs system as gambit-c</div><div><br></div><div>The solution I see is tracking the backward references to variables or functions declared earlier and add a new closure each time a reference is made.</div>
<div>The preceding code would be translated by :</div><div><br></div><div>(define my-module</div><div>  (lambda ()</div><div>    (let ()</div><div>      (define private-fun (lambda (x) ..))</div><div>      (let ()</div><div>
        (define public-var (private-fun 'a-value))</div><div>        (list 'public-var public-var)))))</div><div><br></div><div>Is it the best strategy to deal with this problem ?</div><div>Does the letrec* macro can be written for gambit-c ?</div>
<div><br></div><div>thanks</div><div>cyrille</div><div><br></div><div>references :</div><div>1. Using letrec and lambda to Implement Modules : <a href="http://icem-www.folkwang-hochschule.de/~finnendahl/cm_kurse/doc/schintro/schintro_126.html#SEC163">http://icem-www.folkwang-hochschule.de/~finnendahl/cm_kurse/doc/schintro/schintro_126.html#SEC163</a></div>
<div>2. letrec in the interpreter : <a href="https://mercure.iro.umontreal.ca/pipermail/gambit-list/2005-September/000412.html">https://mercure.iro.umontreal.ca/pipermail/gambit-list/2005-September/000412.html</a></div><div>

</div>