Fibonacci in F#

Published on 08 April 2026 (Updated: 08 April 2026)

Welcome to the Fibonacci in F# page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

open System

module Fibonacci =
    let run n =
        Seq.unfold (fun (a, b) -> Some(a, (b, a + b))) (1, 1)
        |> Seq.take n
        |> Seq.mapi (fun i v -> sprintf "%d: %d" (i + 1) v)
        |> String.concat "\n"

module Helpers =
    let usage = "Usage: please input the count of fibonacci numbers to output"

    let (|NonNegativeInt|_|) (s: string) =
        match s.Trim() |> Int32.TryParse with
        | true, n when n >= 0 -> Some n
        | _ -> None

    let parseArgs argv =
        match argv with
        | [| NonNegativeInt 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 Fibonacci.run |> Helpers.handleResults

Fibonacci in F# was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.