Created
August 2, 2018 06:42
-
-
Save yuchiki/d1ceab5e391c763087804de4a7a08c68 to your computer and use it in GitHub Desktop.
see whether the tail recursion gets optimized or not.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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