Note that there are some explanatory texts on larger screens.

plurals
  1. POCyclic dependency between functions in F#
    primarykey
    data
    text
    <p>I am trying break this cyclic dependency below but not sure the best way to do it.</p> <pre><code>let cashOpeningBalance t = if t = 1 then 0.0 else cashClosingBalance (t - 1) let cashInterest t = cashOpeningBalance t * 0.03 let accumulatedCash t = cashOpeningBalance t + cashInterest t // let moreComplicatedLogic t = ... let cashClosingBalance t = accumulatedCash t </code></pre> <p>Using some logic from this <a href="https://stackoverflow.com/questions/5404735/problem-with-cyclic-dependencies-between-types-and-functions-from-different-file" title="answer">answer</a> I came up with the following solution but the performance is really poor. </p> <pre><code>let cashOpeningBalance t cashClosingBalance = if t = 1 then 10.0 else cashClosingBalance (t - 1) let cashInterest t cashClosingBalance = (cashOpeningBalance t cashClosingBalance) * 0.03 let accumulatedCash t cashClosingBalance = (cashOpeningBalance t cashClosingBalance) + (cashInterest t cashClosingBalance) // let moreComplicatedLogic t cashClosingBalance = ... let rec cashClosingBalance t = //accumulatedCash t cashClosingBalance let temp = accumulatedCash t cashClosingBalance printfn "Cash Closing Balance = %f Where t = %i" temp t temp cashClosingBalance 3 (* &gt; Cash Closing Balance = 10.300000 Where t = 1 Cash Closing Balance = 10.300000 Where t = 1 Cash Closing Balance = 10.609000 Where t = 2 Cash Closing Balance = 10.300000 Where t = 1 Cash Closing Balance = 10.300000 Where t = 1 Cash Closing Balance = 10.609000 Where t = 2 Cash Closing Balance = 10.927270 Where t = 3 val it : float = 10.92727 *) cashClosingBalance 50 (* Takes a really long time *) </code></pre> <p>Is there anyway to rewrite the cashClosingBalance function to stop the excessive recursive calls as seen in the output below? I really need to be able to put in a value of t of up to 400 and it still run in seconds.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload