Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing the same value around in a recursive function?

Tags:

c#

I've got a recursive function with the signature

public static string Recurse(string pattern, ObjectDict dict)

The value of dict never changes. It kind of bothers me that I should have to carry around dozens of references to it, and pass it around each time I call the function again. Is there a way around this?

By "never changes", I mean after the initial call.

like image 578
mpen Avatar asked Nov 24 '10 22:11

mpen


People also ask

Can you have loops in recursion?

You surely can use loops in a recursive function. What makes a function recursive is only the fact that the function calls itself at some point in its execution path. However you should have some condition to prevent infinite recursion calls from which your function can't return.

Why recursion is not always good?

The Bad. In imperative programming languages, recursive functions should be avoided in most cases (please, no hate mail about how this isn't true 100% of the time). Recursive functions are less efficient than their iterative counterparts. Additionally, they are subject to the perils of stack overflows.

Can you use a while loop in a recursive function?

Is a while loop intrinsically a recursion? then, yes, a while loop is a form of recursion. Recursive functions are another form of recursion (another example of recursive definition).

What are the rules used for recursive function?

Like the robots of Asimov, all recursive algorithms must obey three important laws: A recursive algorithm must call itself, recursively. A recursive algorithm must have a base case. A recursive algorithm must change its state and move toward the base case.


3 Answers

references are extremely lightweight, so don't worry about it.

If you really need to avoid it (and, I don't think you do), consider something like this:

class MyClass
{
    private ObjectDict m_dict;

    public string Recurse(string pattern, ObjectDict dict)
    {
        m_dict = dict;
        return HelperRecurse(pattern);
    }

    private string HelperRecurse(string pattern) 
    {
        // do some work. (referring to m_dict)
        HelperRecurse(pattern);  // Go recursive

        return "Hello World";
    }
}

By doing this, you are no longer passing around references to the same dictionary, just always accessing a member-object. However, you've lost the static nature of your function now.

like image 79
abelenky Avatar answered Oct 04 '22 21:10

abelenky


One option is to use a lambda expression to hold the actual logic of the Recurse function. This can then be called recursively and because it's a lambda it will have access to the dict object without having to pass it around

public static string Recurse(string initialPattern, ObjectDict dict) {
  Func<string, string> inner = null;
  inner = pattern => {
    // Logic which uses calls to inner for recursion.  Has access to dict
    // because it's a lambda.  For example
    if (dict.SomeOperation()) { 
      return inner(someOtherPattern);
    }
    return aValue;
  };
  return inner(initialPattern);
}
like image 35
JaredPar Avatar answered Oct 04 '22 23:10

JaredPar


Well, the obvious alternative is to make Recurse an instance method on ObjectDict if you can. Then you can just call Recurse(pattern) internally.

If that's not possible for any reason, don't sweat it. In fact, the two options are really very similar - for instance methods, there's effectively an invisible "this" parameter which is passed along first, before the rest of the parameters. You just happen to have put yours at the other end :)

like image 31
Jon Skeet Avatar answered Oct 04 '22 21:10

Jon Skeet