Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demonstration of how non-tailcall warnings could work in F#
// This code demonstrates how the F# compiler could detect and emit a warning
// for non-tail-recursive calls to help avoid stack overflows.
// In response to: http://fslang.uservoice.com/forums/245727-f-language/suggestions/5663074-enable-a-compiler-warning-when-a-recursive-algorit
module Blah
let foo x =
printfn "Foo: %x" x
let rec bar x =
match x with
| 0 ->
foo x // OK: non-tail-recursive call to a function which doesn't share the current stack frame (i.e., 'bar' or 'baz').
printfn "Zero"
| 1 ->
bar (x - 1) // Warning: this call is not tail-recursive
printfn "Uno"
baz x // OK: tail-recursive call.
| x ->
printfn "0x%08x" x
bar (x - 1) // OK: tail-recursive call.
and baz x =
printfn "Baz!"
bar (x - 1) // OK: tail-recursive call.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.