Palindromic Number in F#

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

Welcome to the Palindromic Number 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 Palindrome =
    let isPalindrome (n: int) =
        if n < 0 then
            false
        elif n % 10 = 0 && n <> 0 then
            false
        else
            let rec loop x rev =
                if x <= rev then
                    x = rev || x = rev / 10
                else
                    loop (x / 10) (rev * 10 + x % 10)

            loop n 0

module Helpers =
    let parseArgs =
        function
        | [| input: string |] ->
            match Int32.TryParse input with
            | true, n when n >= 0 -> Ok n
            | _ -> Error "Usage: please input a non-negative integer"
        | _ -> Error "Usage: please input a non-negative integer"

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

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

Palindromic Number 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.