On Thu, Jan 15, 2009 at 3:34 PM, Phil <span dir="ltr">&lt;<a href="mailto:pbeadling@mail2web.com">pbeadling@mail2web.com</a>&gt;</span> wrote:



<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><font size="4"><font face="Calibri, Verdana, Helvetica, Arial"><span style="font-size: 11pt;"><div class="Ih2E3d">
On 14/01/2009 01:08, &quot;Luke Palmer&quot; &lt;<a href="mailto:lrpalmer@gmail.com" target="_blank">lrpalmer@gmail.com</a>&gt; wrote:<br>
<br>
</div></span></font></font><blockquote><div class="Ih2E3d"><font size="4"><font face="Calibri, Verdana, Helvetica, Arial"><span style="font-size: 11pt;">On Tue, Jan 13, 2009 at 5:45 PM, Phil &lt;<a href="mailto:pbeadling@mail2web.com" target="_blank">pbeadling@mail2web.com</a>&gt; wrote: <br>

</span></font></font><blockquote><font size="4"><font face="Calibri, Verdana, Helvetica, Arial"><span style="font-size: 11pt;">mcSimulate :: Double -&gt; Double -&gt; Word64 -&gt; [Dou<br>
ble]<br>
mcSimulate startStock endTime seedForSeed = fst expiryStock : mcSimulate startStock endTime newSeedForSeed<br>
<br>
It is abundantly clear that the startStock and endTime are just being passed around from call to call unchanged – that is their value is constant throughout the the simulation. &nbsp;For the purposes here when I&#39;m only passing 2 &#39;constants&#39; around it doesn&#39;t strike me as too odd, but my list of &#39;constants&#39; is likely to grow as I bolt more functionality onto this. &nbsp;For readability, I understand that I can create new types to encapsulate complex data types into a single type , but I can&#39;t help thinking that passing say 9 or 10 &#39;constants&#39; around and around like this &#39;feels wrong&#39;. &nbsp;If I sit back and think about it, it doesn&#39;t strike me as implausible that the compiler will recognize what I&#39;m doing and optimize this out for me, and what I&#39;m doing is thinking about the whole think like a C++ programmer (which I traditionally am) would. &nbsp;<br>

</span></font></font></blockquote></div><font size="4"><div class="Ih2E3d"><font face="Calibri, Verdana, Helvetica, Arial"><span style="font-size: 11pt;"><br>
You can factor out constants in a couple ways. &nbsp;If you are just passing constants between a recursive call to the same function, you can factor out the recursive bit into a separate function:<br>
<br>
</span></font></div><span style="font-size: 11pt;"><div class="Ih2E3d"><font face="Courier New">something param1 param2 = go<br>
&nbsp;&nbsp;&nbsp;&nbsp;where<br>
&nbsp;&nbsp;&nbsp;&nbsp;go = ... param1 ... param2 ... etc ... go ... <br>
&nbsp;&nbsp;&nbsp;&nbsp;etc = ...<br>
</font></div><font face="Calibri, Verdana, Helvetica, Arial"><div class="Ih2E3d"><br>
Where go takes only the parameters that change, and the rest is handled by its enclosing scope. &nbsp;You might buy a little performance this way too, depending on the compiler&#39;s cleverness (I&#39;m not sure how it optimizes these things).<br>

<br>
<br></div>
[PHIL]<br>
Firstly – thanks for your advice.<br>
<br>
When I say constants, I should be clear – these are parameters passed in by the user, but they remain constant throughout the recursive call. &nbsp;I think the example above is only relevant if they are constants at compile time? &nbsp;If not I'm not sure I follow the example. &nbsp;If we have something like<br>

<br>
</font></span><div class="Ih2E3d"><font color="#00007f"><font face="Arial"><span style="font-size: 10pt;">mcSimulate :: Double -&gt; Double -&gt; Word64 -&gt; [Double]<br>
mcSimulate startStock endTime seedForSeed = fst expiryStock : mcSimulate</span></font></font></div></font><font size="5"><font face="Times New Roman"><span style="font-size: 12pt;"> </span></font></font><font color="#00007f"><font size="4"><font face="Arial"><span style="font-size: 10pt;">startStock endTime newSeedForSeed<br>

</span></font></font></font><div class="Ih2E3d"><font size="5"><font face="Times New Roman"><span style="font-size: 12pt;"> &nbsp;</span></font></font><font color="#00007f"><font size="4"><font face="Arial"><span style="font-size: 10pt;">where<br>

</span></font></font></font><font size="5"><font face="Times New Roman"><span style="font-size: 12pt;"> &nbsp;&nbsp;&nbsp;</span></font></font><font color="#00007f"><font size="4"><font face="Arial"><span style="font-size: 10pt;">expiryStock = iterate evolveUnderlying (startStock, ranq1Init seedForSeed) !! truncate (endTime/timeStep)<br>

</span></font></font></font><font size="5"><font face="Times New Roman"><span style="font-size: 12pt;"> &nbsp;&nbsp;&nbsp;</span></font></font><font color="#00007f"><font size="4"><font face="Arial"><span style="font-size: 10pt;">newSeedForSeed = seedForSeed + 246524<br>

<br>
</span></font></font></font></div><font size="4"><font face="Calibri, Verdana, Helvetica, Arial"><span style="font-size: 11pt;">Here startStock and endTime are not altered from iteration to iteration, but they are not known at compile time. &nbsp;I see that I can reduce this to something like<br>

<br>
</span></font><font color="#00007f"><font face="Arial"><span style="font-size: 10pt;">test seedForSeed = fst expiryStock : test newSeedForSeed<br>
&nbsp;&nbsp;where<br>
</span></font></font></font><div class="Ih2E3d"><font size="5"><font face="Times New Roman"><span style="font-size: 12pt;"> &nbsp;&nbsp;&nbsp;</span></font></font><font color="#00007f"><font size="4"><font face="Arial"><span style="font-size: 10pt;">expiryStock = iterate evolveUnderlying (_startStock, ranq1Init seedForSeed) !! truncate (_endTime/timeStep)<br>

</span></font></font></font><font size="5"><font face="Times New Roman"><span style="font-size: 12pt;"> &nbsp;&nbsp;&nbsp;</span></font></font><font color="#00007f"><font size="4"><font face="Arial"><span style="font-size: 10pt;">newSeedForSeed = seedForSeed + 246524<br>

<br>
</span></font></font></font></div><font size="4"><font face="Calibri, Verdana, Helvetica, Arial"><span style="font-size: 11pt;">But don't understand how I 'feed' the _startStock and _endTime in?<br>
<br>
Could you explain this in detail, or confirm my suspicions that it only works for compile-time constants?</span></font></font></blockquote></div></blockquote><div><br>Compile-time constants could be handled by simple top-level bindings.&nbsp; This technique is specifically for the case you are after:<br>
<br style="font-family: courier new,monospace;"><font style="color: rgb(0, 0, 0); font-family: courier new,monospace;" size="4"><div class="Ih2E3d"><span style="font-size: 10pt;">mcSimulate :: Double -&gt; Double -&gt; Word64 -&gt; [Double]<br>

mcSimulate startStock endTime seedForSeed = go seedForSeed</span></div></font><font style="color: rgb(0, 0, 0); font-family: courier new,monospace;" color="#00007f"><font size="4"><span style="font-size: 10pt;"></span></font></font><font style="color: rgb(0, 0, 0); font-family: courier new,monospace;" size="5"><span style="font-size: 12pt;">&nbsp; </span></font><span style="color: rgb(0, 0, 0); font-family: courier new,monospace;">where</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; go = fst expiryStock : go newSeedForSeed</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where</span><br style="color: rgb(0, 0, 0); font-family: courier new,monospace;">
<div style="color: rgb(0, 0, 0); font-family: courier new,monospace;" class="Ih2E3d"><font size="4"><span style="font-size: 10pt;">
</span></font><font size="5"><span style="font-size: 12pt;"> &nbsp;&nbsp;&nbsp;&nbsp; </span></font><font size="4"><span style="font-size: 10pt;">expiryStock = iterate evolveUnderlying (startStock, ranq1Init seedForSeed) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; !! truncate (endTime/timeStep)<br>

</span></font><font size="5"><span style="font-size: 12pt;"> &nbsp; &nbsp;&nbsp; </span></font><font size="4"><span style="font-size: 10pt;">newSeedForSeed = seedForSeed + 246524<br>
</span></font></div> <br style="font-family: courier new,monospace;">See what&#39;s going on there?<br><br>I don&#39;t know about that nested where.&nbsp; In Real Life I would probably use a let instead for expiryStock and newSeedForSeed.<br>
<br>Luke<br></div></div><br>