Skip to content

Instantly share code, notes, and snippets.

@MikeMKH
Created July 12, 2016 18:05
Show Gist options
  • Save MikeMKH/f5e2bf37b384308d25d54793d9e4f752 to your computer and use it in GitHub Desktop.
Save MikeMKH/f5e2bf37b384308d25d54793d9e4f752 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) a)
{
if (a.n <= 1) return a.acc;
var r = (a.acc * a.n, a.n - 1);
return Aux(r);
}
(int, int) t = (1, x);
return Aux(t);
}
Console.WriteLine($"1! = {Factorial(1)}");
Console.WriteLine($"5! = {Factorial(5)}");
Console.WriteLine($"20! = {Factorial(20)}");
Console.ReadKey();
}
}
}
@MikeMKH
Copy link
Author

MikeMKH commented Jul 12, 2016

Same code as https://gist.github.com/MikeMKH/7488b5ce907bb58912f7d4d476ebdf48 but playing with defining the Tuples types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment