<p dir="ltr">You could also hide the data with a monad.</p>
<div class="gmail_quote">On Jul 19, 2013 9:35 AM, "Carl Eastlund" <<a href="mailto:cce@ccs.neu.edu">cce@ccs.neu.edu</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div><div><div><div><div><div><div><div><div><div>One common solution to this issue is a parameter:<br><br></div>(define current-data (make-parameter #f))<br><br></div>(define (f1 the-data ...)<br></div> (parameterize ([current-data the-data])<br>
</div> (f2 ...)))<br><br></div>(define (f2 ...)<br></div> ... (f3 ...) ...)<br><br>...<br><br></div>(define (fx ...)<br></div> ... (current-data) ...)<br><br></div>The parameterize form in f1 means that calls to (current-data) will produce the-data during the dynamic extend of f1 (meaning from the time f1 is called until it returns). This isn't just a set! on enter and exit; different threads won't see this change, for instance, and if f1 is exited/re-entered using a continuation, (current-data) will only produce the-data while inside f1. So while it's not purely functional, it's a much more disciplined kind of effect.<br>
</div><div><div class="gmail_extra"><br clear="all"><div>Carl Eastlund</div>
<br><div class="gmail_quote">On Fri, Jul 19, 2013 at 12:23 PM, Ben Duan <span dir="ltr"><<a href="mailto:yfefyf@gmail.com" target="_blank">yfefyf@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div>Scenario: A piece of data is determined in the first function `f1', but is only processed in a sub-sub-sub-… function `fx'.</div><div><br></div><div>One way is to use pass `the-data' as arguments from `f1' through `f2' all the way down to `fx':</div>
<div><br></div><div><div><font face="courier new, monospace"> (define f1 (the-data …)</font></div><div><font face="courier new, monospace"> …</font></div><div><font face="courier new, monospace"> (f2 the-data …)</font></div>
<div><font face="courier new, monospace"> …)</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> (define f2 (the-data …)</font></div><div><font face="courier new, monospace"> …</font></div>
<div><font face="courier new, monospace"> (f3 the-data …)</font></div><div><font face="courier new, monospace"> …)</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> …</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> (define fx (the-data …)</font></div><div><font face="courier new, monospace"> … the-data …)</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="arial, helvetica, sans-serif">But in the above way, the body of `f2', `f3', `f4'</font><span style="font-family:arial,helvetica,sans-serif"> and so on doesn't use `the-data'. It is only passed to the next function. And I still have to add the argument `the-data'.</span></div>
<div><span style="font-family:arial,helvetica,sans-serif"><br></span></div><div><span style="font-family:arial,helvetica,sans-serif">Another way is to use `set!':</span></div><div><br></div><div><font face="courier new, monospace"> (define the-data …)</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> (define f1 (the-data …)</font></div><div><font face="courier new, monospace"> …</font></div><div><font face="courier new, monospace"> (set! the-data …)</font></div>
<div><font face="courier new, monospace"> …</font></div><div><font face="courier new, monospace"> (f2 …)</font></div><div><font face="courier new, monospace"> …)</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace"> (define f2 (…)</font></div><div><font face="courier new, monospace"> </font><span style="font-family:'courier new',monospace">…</span></div><div><font face="courier new, monospace"> (f3 </font><span style="font-family:'courier new',monospace">…)</span></div>
<div><font face="courier new, monospace"> …)</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> …</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace"> (define fx (…)</font></div><div><font face="courier new, monospace"> … the-data …)</font></div></div><div><br></div><div><font face="arial, helvetica, sans-serif">But in this way, the benefits of being functional are lost. For example there will be some problems writing tests for these functions.</font></div>
<div><br></div><div><font face="arial, helvetica, sans-serif">My question is, which way is better? Or are there other ways to solve this problem?</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div>
<div><font face="arial, helvetica, sans-serif">Thanks,</font></div><div><font face="arial, helvetica, sans-serif">Ben</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">P.S. This question is not about Racket. It's just a beginner's question about how to program. Please let me know if it's not appropriate to ask this kind of questions here. Thank you.</font></div>
</div>
<br>____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
<br></blockquote></div><br></div></div></div>
<br>____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
<br></blockquote></div>