Skip to content

Instantly share code, notes, and snippets.

@jack-pappas
Created March 29, 2014 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jack-pappas/9860949 to your computer and use it in GitHub Desktop.
Save jack-pappas/9860949 to your computer and use it in GitHub Desktop.
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