Created
March 29, 2014 19:04
-
-
Save jack-pappas/9860949 to your computer and use it in GitHub Desktop.
Demonstration of how non-tailcall warnings could work in F#
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
// 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