Skip to content

Instantly share code, notes, and snippets.

@yuchiki
Created August 2, 2018 06:42
Show Gist options
  • Save yuchiki/d1ceab5e391c763087804de4a7a08c68 to your computer and use it in GitHub Desktop.
Save yuchiki/d1ceab5e391c763087804de4a7a08c68 to your computer and use it in GitHub Desktop.
see whether the tail recursion gets optimized or not.
using System;
using static System.Console;
using System.Diagnostics;
using System.Linq;
class A {
static void Main() => WriteLine($"Sum:{Sum(10)}");
static int Sum(int n) => _sum(0, n);
static int _sum(int buf, int m) {
ShowCallStack();
return m == 0 ? buf : _sum(buf + m, m - 1);
}
static void ShowCallStack() {
var funcNames =
new StackTrace().GetFrames().Skip(1).Reverse().Select(frame => frame.GetMethod().Name);
WriteLine(String.Join(" -> ", funcNames));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment