XCODX |

OCaml Online Compiler & Interpreter

Select Language
Online Code Compiler
Full HTML IDE
Py main.py
Program Output Ready
  Welcome to XCODX Online Compiler

  Quick Start:
  Ctrl+Enter  Run code
  Ctrl+S      Save / Download
  Ctrl+L      Clear output

  Select a language and start coding.
Success
Operation completed

About OCaml

OCaml is a statically typed language from the ML family that blends functional, imperative, and object-oriented styles under one type system with full type inference. It grew out of the Caml project at France's INRIA institute, with the Objective Caml release arriving in 1996 under Xavier Leroy and colleagues. Its type checker catches many errors at compile time without requiring type annotations, and its native compiler is known for fast code. OCaml is used where correctness and performance both matter: the Jane Street trading firm builds most of its systems in it, the first Rust compiler was written in it, and tools like Facebook's Flow and the Coq proof assistant are built on it. On XCODX, OCaml runs in a cloud sandbox through the ocaml interpreter, so you can try the language and its standard library without installing opam or a build tool.

Hello World in OCaml

(* OCaml stdlib: read a line, then fold a list *)
let () =
  print_string "What is your name? ";
  let name = read_line () in
  Printf.printf "Hello, %s!\n" name;
  let total = List.fold_left ( + ) 0 [1; 2; 3; 4; 5] in
  Printf.printf "Sum of 1..5 is %d\n" total

When to use OCaml

OCaml fits problems where you want strong compile-time guarantees and native-code speed at the same time, such as compilers, static analyzers, theorem provers, and low-latency financial systems. Pattern matching over algebraic data types makes modeling complex states concise, and the compiler checks that matches are exhaustive. Its module system supports large codebases with clear interfaces. It is a less obvious pick for quick scripting or teams that need a large ready-made library ecosystem, since its package set is smaller than those of Python or JavaScript.

Common questions

How do I read input in OCaml?

read_line () returns the next line from standard input as a string, Scanf.scanf reads typed values with a format such as %d, and read_int () parses a whole line as an int. On XCODX you can type into the live terminal or paste into the Stdin Box up front, and Ctrl+D closes the stream.

Can I install libraries with opam?

No. The sandbox runs a single file against the OCaml standard library only and has no network access, so there is no opam and no dune build. Packages like Core, Base, or Lwt cannot be fetched; use stdlib modules such as List, Array, String, Printf, and Scanf.

Is OCaml compiled to native code here?

It runs through the ocaml interpreter, which evaluates the file rather than producing a standalone native binary the way ocamlopt would. You still get full type checking before execution, so type errors stop the program before any output appears.

Why do I get a type error before my program runs?

OCaml is statically typed with type inference, so the whole file is checked before it runs. A mismatch, such as passing a string where an int is expected or a non-exhaustive match, is reported at compile time with a location, and nothing executes until it is fixed.

Why does my prompt appear after I type, and how do I print without a newline?

Output is buffered, so a prompt printed with print_string may not show until the buffer flushes; call flush stdout before reading to force it out. print_string writes without a trailing newline, print_endline adds one, and Printf.printf uses format strings.

Which OCaml version does XCODX run?

The OCaml version is shown in the badge next to the editor. It is free with no signup and no local install, and each run starts from a fresh sandbox with no saved files.

How OCaml runs on XCODX

Sandbox filename
main.ml
Entry point
single main source file
Editor grammar
mllike
Reading stdin
read_line()
Input delivery
live WebSocket stream
Prompt flushing
flush manually before reading input
Compile limit
10 s
Run limit
3 s batch · up to 3 min live
Memory
256 MB per stage
Max source
50,000 characters

Default program on this page

print_endline "Hello from OCaml!";;
print_endline "Welcome to XCODX Online Compiler!";;
let message = "OCaml is functional!";;
print_endline message;;