Sleep Sort in F#

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

Welcome to the Sleep Sort 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
open System.Collections.Concurrent

module SleepSort =
    let run numbers =
        let results = ConcurrentQueue<int>()

        let tasks =
            numbers
            |> List.map (fun n ->
                async {
                    do! Async.Sleep(n * 100)
                    results.Enqueue n
                })

        tasks |> Async.Parallel |> Async.RunSynchronously |> ignore
        results |> Seq.toList |> List.map string |> String.concat ", " |> Ok

module Result =
    let toOption =
        function
        | Ok x -> Some x
        | Error _ -> None

module Helpers =
    let private (|IntList|_|) (s: string) =
        let ns =
            s.Split(',', StringSplitOptions.RemoveEmptyEntries)
            |> Array.toList
            |> List.map (fun p ->
                match Int32.TryParse(p.Trim()) with
                | true, n -> Ok n
                | false, _ -> Error $"Invalid integer: '{p}'")
            |> List.choose Result.toOption

        if ns.Length >= 2 then Some ns else None

    let parseArgs argv =
        match argv with
        | [| IntList numbers |] -> Ok numbers
        | _ -> Error "Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\""

    let handleResult =
        function
        | Ok result ->
            printfn "%s" result
            0
        | Error msg ->
            eprintfn "%s" msg
            1

[<EntryPoint>]
let main argv =
    argv |> Helpers.parseArgs |> Result.bind SleepSort.run |> Helpers.handleResult

Sleep Sort 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.