Remove All Whitespace in OCaml

Published on 25 May 2026 (Updated: 25 May 2026)

Welcome to the Remove All Whitespace in OCaml page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

(* Use custom function instead of Char.Ascii.is_white because the builtin also matches 
   on vertical tab and form feed which are not considered whitespace for the 
   purposes of this problem *)
let is_whitespace = function ' ' | '\t' | '\n' | '\r' -> true | _ -> false

let remove_whitespace s =
  String.to_seq s |> Seq.filter (Fun.negate is_whitespace) |> String.of_seq

let () =
  print_endline
  @@
  match Sys.argv with
  | [| _; s |] when s <> "" -> remove_whitespace s
  | _ -> "Usage: please provide a string"

Remove All Whitespace in OCaml 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.