Skip to content

Instantly share code, notes, and snippets.

@MikeMKH
Created July 12, 2016 12:35
Show Gist options
  • Save MikeMKH/7488b5ce907bb58912f7d4d476ebdf48 to your computer and use it in GitHub Desktop.
Save MikeMKH/7488b5ce907bb58912f7d4d476ebdf48 to your computer and use it in GitHub Desktop.
C# 7 Local Function example with tuples in a tail call
using System;
// Visual Studio 15 Preview 3
// Conditional compilation symbols: __DEMO__ ,__DEMO_EXPERIMENTAL__
// Manually added ValueTuples.cs to project https://raw.githubusercontent.com/dotnet/roslyn/future/docs/features/ValueTuples.cs
namespace TupleExamples
{
class Program
{
static void Main(string[] args)
{
long Factorial(int x)
{
long Aux((int acc, int n) _)
{
if (_.n <= 1) return _.acc;
return Aux((_.acc * _.n, _.n - 1));
}
return Aux((1, x));
}
Console.WriteLine($"1! = {Factorial(1)}");
Console.WriteLine($"5! = {Factorial(5)}");
Console.WriteLine($"20! = {Factorial(20)}");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment