(* Tail Recursion Experiment While loops and for loops are available in F#. However, with the pattern-matching style of programming recursion is often the most natural way to write a function. Therefore it is crucial to distinguish between tail and non-tail recursion. This experiment will demonstrate that F# will optimize tail recursion like Scheme and C. Java/C# does not optimize tail recursion (except for specialized compilers) because of dynamic dispatch: the version of the function called is not determined at compile time. However, F#'s pattern matching mechanism replaces dynamic dispatch. *) open System;; let rec f = function | n when n<2 -> n | n -> 1 + f(n-1);; let rec g = function | n when n<2 -> printfn "done" | n -> if n%100=0 then printf "%d " n; g(n-1);; // which one is tail-recursive? g. g(1000000);; // runs without problems because of tail recursion optimization let x = f(1000000);; // will crash with stack overflow exception