<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><br></div><div>There are many ways to solve this problem and you have heard about two: </div><div><br></div><div>-- parameters (looks almost functional) </div><div>-- a monad (presumably using macros and well out of reach for a true beginner)</div><div><br></div><div>Here are some more in order of preference: </div><div><br></div><div>-- if you are willing to compute the data at the module level -- widening its scope -- do so and think of the-data as a constant </div><div>-- If x is a small number (3 - 5), I would try to use (lexical) scope. That is, I would place f2 through fx in the scope of the-data (local to f1) </div><div>-- if x is large (> 5), consider using a unit and invoking it locally. This is NOT a beginner question. </div><div><br></div><div>All of these solutions are functional and cost nearly nothing (as is the monad solution). </div><div><br></div><div>If you ever consider assigning to the-data, use the solution you proposed or Carl's, depending on precise needs. </div><div><br></div><div><br></div><div><br></div><div><br></div><br><div><div>On Jul 19, 2013, at 12:23 PM, Ben Duan wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><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> Racket Users list:<br> <a href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a><br></blockquote></div><br></body></html>