<div dir="ltr"><div><div><div><div><div><div>Hello,<br><br></div><div>as an extension of the question of Ben.<br></div><div><br></div>(define (func args)<br></div> (define result init-data)<br></div> (define (loop args-loop)<br>
...<br></div> (set! result some-data)<br> ....)<br></div> (loop args)<br></div> result)<br><div><div><div><br></div><div>Is this a good practice?<br></div><div>And if you ask why don't directly return loop, it is for the case to make recursive call every where in the function not only in terminal possition.<br>
</div><div><div><div><br></div></div></div></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/7/19 Joe Marshall <span dir="ltr"><<a href="mailto:jmarshall@alum.mit.edu" target="_blank">jmarshall@alum.mit.edu</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir="ltr">You could also hide the data with a monad.</p><div class="HOEnZb"><div class="h5">
<div class="gmail_quote">On Jul 19, 2013 9:35 AM, "Carl Eastlund" <<a href="mailto:cce@ccs.neu.edu" target="_blank">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>
</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>