Last active
December 10, 2020 00:29
-
-
Save 0918nobita/b89b212028439c7d68fd4da016bc82c6 to your computer and use it in GitHub Desktop.
Tail-call optimization
This file contains hidden or 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 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 $_ | |
| { | |
| } | |
| } |
This file contains hidden or 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
| 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