Can you help me understand this example of fibonacci using C# closures? -


While trying to learn functional C #, I put Dustin Campbell in C # 3. My solution works but I have trouble understanding the last line of NT_FIB. Is not it strange that there is no difference to the formula? N is immediately bound in expression "fn => nth_fib (n - 1) + nth_fib (n - 2)" but i have the function & amp; Lt; Int & gt; Can not change the type of return on Does it mean I have to pass an unnecessary argument later?

Note: Exam pass

  [TestFixture] Public square testfilm {{test} Public Zero test_fib () {Hon. AreEqual (8, New Fibliaccier). Nth_Fib 5)); Confirm. Eraquall (1597, new Fiberclosure (). NTFIF (16)); }} Public Square Fibolos {Private Func & lt; Int, int & gt; [] FormulaCache; Public int Nth_Fib (int n) {if (n & lt; = 2) return n; If (formula cache == empty) formula = new fax & lt; Int, int & gt; [(N + 1)]; Function & lt; Int, int & gt; Formula = build (n); // No matter, n is already bound !! ?? Return Formula (334567); // personal form such as Formula (N)} & lt; Int, int & gt; Construct (int n) {if (formulaCache [n] == null] formula cache [n] = (fn = & gt; nth_fib (n - 1) + nth_fib (n - 2)); Return Formula Cachees [N]; }}  

you can type type to Func & lt; Int & gt; - You just have to do this everywhere:

  Public class Fibocus {Private Func & lt; Int & gt; [] FormulaCache; Public int Nth_Fib (int n) {if (n & lt; = 2) return n; If (formula cache == empty) formula = new fax & lt; Int & gt; [(N + 1)]; & Lt; Integer & gt; Formula = build (n); Return formula (); } Private Fun & lt; Int & gt; Construction (int n) {if (formulaCache [n] == null) formulaCache [n] = () = & gt; Nth_Fib (N-1) + NT_Fib (N-2); Return Formula Cachees [N]; Logic is not required for the function, because the computation of Fibonacci value is fixed - basically the  formulaCache [x]  calculates  false (x ) . The clue for this was the fact that you did not use any code in  fn   build  within lambda expression anyway. 

I am not sure that I would say that this is a very clear way of structuring things, of course ...


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -