Longest Word in F#

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

Welcome to the Longest Word 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 LongestWord =
    let run (s: string) =
        s
        |> Seq.fold
            (fun (maxLen, currLen) ch ->
                if Char.IsWhiteSpace ch then
                    (max maxLen currLen, 0)
                else
                    (maxLen, currLen + 1))
            (0, 0)
        |> fun (maxLen, currLen) -> max maxLen currLen

module Helpers =
    let usage = "Usage: please provide a string"

    let parseArgs argv =
        match argv with
        | [| s |] when not (String.IsNullOrEmpty s) -> Ok s
        | _ -> Error usage

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

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

Longest Word 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.