On Thu, Jan 15, 2009 at 3:34 PM, Phil <span dir="ltr"><<a href="mailto:pbeadling@mail2web.com">pbeadling@mail2web.com</a>></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, "Luke Palmer" <<a href="mailto:lrpalmer@gmail.com" target="_blank">lrpalmer@gmail.com</a>> 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 <<a href="mailto:pbeadling@mail2web.com" target="_blank">pbeadling@mail2web.com</a>> wrote: <br>
</span></font></font><blockquote><font size="4"><font face="Calibri, Verdana, Helvetica, Arial"><span style="font-size: 11pt;">mcSimulate :: Double -> Double -> Word64 -> [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. For the purposes here when I'm only passing 2 'constants' around it doesn't strike me as too odd, but my list of 'constants' is likely to grow as I bolt more functionality onto this. For readability, I understand that I can create new types to encapsulate complex data types into a single type , but I can't help thinking that passing say 9 or 10 'constants' around and around like this 'feels wrong'. If I sit back and think about it, it doesn't strike me as implausible that the compiler will recognize what I'm doing and optimize this out for me, and what I'm doing is thinking about the whole think like a C++ programmer (which I traditionally am) would. <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. 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>
where<br>
go = ... param1 ... param2 ... etc ... go ... <br>
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. You might buy a little performance this way too, depending on the compiler's cleverness (I'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. I think the example above is only relevant if they are constants at compile time? If not I'm not sure I follow the example. 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 -> Double -> Word64 -> [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;"> </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;"> </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;"> </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. 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>
where<br>
</span></font></font></font><div class="Ih2E3d"><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;">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;"> </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. 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 -> Double -> Word64 -> [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;"> </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;"> go = fst expiryStock : go newSeedForSeed</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> 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;"> </span></font><font size="4"><span style="font-size: 10pt;">expiryStock = iterate evolveUnderlying (startStock, ranq1Init seedForSeed) <br> !! truncate (endTime/timeStep)<br>
</span></font><font size="5"><span style="font-size: 12pt;"> </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's going on there?<br><br>I don't know about that nested where. In Real Life I would probably use a let instead for expiryStock and newSeedForSeed.<br>
<br>Luke<br></div></div><br>