Last active
February 27, 2024 08:04
-
-
Save gusty/11113593 to your computer and use it in GitHub Desktop.
Trampolines in F#
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
let trampoline f c n = | |
let rec loop = function | |
| Choice1Of2 x -> x | |
| Choice2Of2 x -> loop (f x) | |
loop (Choice2Of2 (c,n)) | |
// Test | |
let factorial n = | |
let rec factorialT (current, n) = | |
if n = bigint 0 then Choice1Of2 current | |
else Choice2Of2 (current * n, n - 1I) | |
trampoline factorialT 1I n | |
factorial 20000I |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment