Skip to content

Instantly share code, notes, and snippets.

@0918nobita
Last active December 10, 2020 00:29
Show Gist options
  • Select an option

  • Save 0918nobita/b89b212028439c7d68fd4da016bc82c6 to your computer and use it in GitHub Desktop.

Select an option

Save 0918nobita/b89b212028439c7d68fd4da016bc82c6 to your computer and use it in GitHub Desktop.
Tail-call optimization
using Microsoft.FSharp.Core;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: FSharpInterfaceDataVersion(2, 0, 0)]
[assembly: AssemblyVersion("0.0.0.0")]
[CompilationMapping(SourceConstructFlags.Module)]
public static class @_
{
[Serializable]
internal sealed class factCPS@8<a> : FSharpFunc<int, a>
{
public int n;
public FSharpFunc<int, a> k;
[CompilerGenerated]
[DebuggerNonUserCode]
internal factCPS@8(int n, FSharpFunc<int, a> k)
{
this.n = n;
this.k = k;
}
public override a Invoke(int x)
{
return k.Invoke(n * x);
}
}
public static int fact(int n)
{
if (n > 0)
{
return n * fact(n - 1);
}
return 1;
}
[CompilationArgumentCounts(new int[] {
1,
1
})]
public static a factCPS<a>(int n, FSharpFunc<int, a> k)
{
while (n <= 0)
{
int num = n - 1;
k = new factCPS@8<a>(n, k);
n = num;
}
return k.Invoke(1);
}
}
namespace <StartupCode$_>
{
internal static class $_
{
}
}
open System.Diagnostics
let rec fact n = if n > 0 then n * (fact (n - 1)) else 1
let rec factCPS n k =
if n > 0
then k 1
else factCPS (n - 1) (fun x -> k (n * x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment