Recursion with Func
- by David in Dakota
Is it possible to do recursion with an Func delegate? I have the following, which doesn't compile because the name of the Func isn't in scope...
Func<long, long, List<long>, IEnumerable<long>> GeneratePrimesRecursively = (number, upperBound, primeFactors) =>
{
if (upperBound > number)
{
return new List<long>();
}
else
{
if (!primeFactors.Any(factor => number % factor == 0)) primeFactors.Add(number);
return GeneratePrimesRecursively(++number, upperBound, primeFactors); // breaks here.
}
};