Last active
July 21, 2021 22:34
-
-
Save idkjs/454418678465bebb45a3f5f1833213e1 to your computer and use it in GitHub Desktop.
Using the `@tailcall` annotation in ReasonML
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
let sum = n => { | |
let rec loop = (n, k) => | |
if (n == 0) { | |
k(0); | |
} else { | |
([@tailcall] loop)(n - 1, x => k(x + n)); | |
}; | |
([@tailcall] loop)(n, x => x); | |
}; |
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
let sum = n => { | |
let rec loop = (n, k) => | |
if (n == 0) { | |
k(0); | |
} else { | |
loop(n - 1, x => k(x + n)); | |
}; | |
loop(n, x => x); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do you test if this reasonml function is tail recursive. The simple version show you the unannottated function and
usingTailCall.re
show its annotated in reasonml.Call in terminal with:
ocamlc -annot -o tailCall -pp "refmt -p ml" -impl tailCall.re
You can check where the tail calls are with:
Source: https://stackoverflow.com/a/40401233/2336356
Sketch: https://sketch.sh/s/eBl2Z0Wvd21azKu6SFzb9j/