Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 592603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:45:25+00:00 2026-05-13T15:45:25+00:00

Attempting to learn functional C#, I ported Dustin Campbell’s fibonacci example to C#3. My

  • 0

Attempting to learn functional C#, I ported Dustin Campbell’s fibonacci example to C#3. My solution works but I have trouble understanding the last line of Nth_Fib. Isn’t it weird that the agrument to formula doesn’t matter? n gets instantly bound in the expression “fn => Nth_Fib(n – 1) + Nth_Fib(n – 2)” but I can’t change the return type of build to Func<int>. This means I have to pass a redundant argument later?

note: the test passes

[TestFixture]
public class TestFibClosure{
    [Test]
    public void test_fib(){
        Assert.AreEqual(8, new FibClosure().Nth_Fib(5));
        Assert.AreEqual(1597, new FibClosure().Nth_Fib(16));
    }
}

public class FibClosure{
    private Func<int, int>[] formulaCache;

    public int Nth_Fib(int n){
        if (n <= 2) return n;
        if(formulaCache == null) formulaCache = new Func<int, int>[(n + 1)];
        Func<int, int> formula = build(n);
        //doesn't matter, n is already bound !! ??
        return formula(334567);//same as formula(n)
    }

    private Func<int, int> build(int n){
        if (formulaCache[n] == null)
            formulaCache[n] = (fn => Nth_Fib(n - 1) + Nth_Fib(n - 2));
        return formulaCache[n];
    }
}
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:45:25+00:00Added an answer on May 13, 2026 at 3:45 pm

    You can change the type to Func<int> – you’ve just got to do it everywhere:

    public class FibClosure{
        private Func<int>[] formulaCache;
    
        public int Nth_Fib(int n){
            if (n <= 2) return n;
            if(formulaCache == null) formulaCache = new Func<int>[(n + 1)];
            Func<int> formula = build(n);
            return formula();
        }
    
        private Func<int> build(int n){
            if (formulaCache[n] == null)
                formulaCache[n] = () => Nth_Fib(n - 1) + Nth_Fib(n - 2);
            return formulaCache[n];
        }
    }
    

    You don’t need an argument for the function, because the Fibonacci value it’s computing is fixed – basically formulaCache[x] will compute fib(x). The clue to this was the fact that you didn’t use fn within the lambda expression in build anyway.

    I’m not sure I’d say this is a terribly clear way of structuring things, admittedly…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to learn Spring MVC but have been spoiled by Grails. While I
I'm attempting to learn Scala so I have decided to implement datastructures with it.
I'm trying to learn python and I'm attempting a hangman game. But when I
I am attempting to learn about creating windows in c++, I have looked at
I am attempting to learn how to develop on the Android platform but do
I'm following this tutorial attempting to learn XNA, but I'm having issues making my
I'm attempting to learn OOP and have a few questions. I've read the first
Attempting to learn CI and going through the docs to get a better understanding.
I'm attempting to learn myself up on F#, and I fear I'm not understanding
I'm attempting to learn Haskell by writing a SIP client. The question I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.