Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Recursion in F#
open System open System.Diagnostics let time fn = let s = Stopwatch() s.Start () fn () |> ignore s.Stop () s.Elapsed //Regular recursion let rec fact = function | 1 -> 1 | n when n <= 0 -> failwith "Negative factorial" | n -> n * fact (n - 1) //Tail recursion let fact_tail num = let rec loop = function | 1 -> 1 | n when n <= 0 -> failwith "Negative factorial" | n -> num * loop (n - 1) loop num time (fun () -> fact 2000000) |> string |> printfn "Regular recursion time: %s" time (fun () -> fact_tail 2000000) |> string |> printfn "Tail recursion time: %s"
run
|
edit
|
history
|
help
0
Give F# Some Love!
KR#2
Sending an email
F#: Basic Immutability vs. Mutability
Average Square Error
The wonders of F#
Split list without mutable values
code di ia
FizzBuzz without Modulo in F#
Highly Modular FizzBuzz