A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Factorial in F# page! Here, you'll find the source code for this program as well as a description of how the program works.
open System
module Factorial =
let rec private factorial n acc =
if n <= 1L then acc else factorial (n - 1L) (acc * n)
let run n = factorial n 1L |> string
module Helpers =
let usage = "Usage: please input a non-negative integer"
let (|NonNegativeInteger|_|) (s: string) =
match s.Trim() |> Int64.TryParse with
| true, n when n >= 0L -> Some n
| _ -> None
let parseArgs argv =
match argv with
| [| NonNegativeInteger n |] -> Ok n
| _ -> Error usage
let handleResults =
function
| Ok result ->
printfn "%s" result
0
| Error msg ->
eprintfn "%s" msg
1
[<EntryPoint>]
let main argv =
argv |> Helpers.parseArgs |> Result.map Factorial.run |> Helpers.handleResults
Factorial in F# was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.