OCaml Online Compiler - Test OCaml Code Without Installing opam
Jane Street, one of the largest proprietary trading firms in the world, writes almost all of their production code in OCaml. That single fact tells you something about the language: it's serious, it's fast, and its type system catches real bugs before code ever runs.
OCaml belongs to the ML family of languages. It has type inference that feels almost magical: you write a function, and the compiler figures out the types without you annotating anything. But when something goes wrong, those inferred types give you precise error messages pointing exactly to where your logic broke.
Setting up OCaml locally means installing opam (the package manager), configuring a switch, pulling in dune for builds. It works, but it's not quick. An OCaml online compiler gets you from zero to running code in about 5 seconds.
Pattern matching and type inference in action
This is what OCaml looks like when it's doing what it does best. Try it on OneCompiler:
(* A small expression evaluator - no type annotations needed *)
type expr =
| Num of float
| Add of expr * expr
| Mul of expr * expr
| Neg of expr
let rec eval = function
| Num n -> n
| Add (a, b) -> eval a +. eval b
| Mul (a, b) -> eval a *. eval b
| Neg e -> -.(eval e)
let rec to_string = function
| Num n -> string_of_float n
| Add (a, b) -> "(" ^ to_string a ^ " + " ^ to_string b ^ ")"
| Mul (a, b) -> "(" ^ to_string a ^ " * " ^ to_string b ^ ")"
| Neg e -> "-(" ^ to_string e ^ ")"
let () =
(* Build expression: -(3 + 4) * 2 *)
let expr = Mul (Neg (Add (Num 3.0, Num 4.0)), Num 2.0) in
Printf.printf "%s = %g\n" (to_string expr) (eval expr);
(* Build expression: (1.5 + 2.5) + (3 * 4) *)
let expr2 = Add (Add (Num 1.5, Num 2.5), Mul (Num 3.0, Num 4.0)) in
Printf.printf "%s = %g\n" (to_string expr2) (eval expr2)
Notice there are zero type annotations on eval or to_string. OCaml infers that eval takes an expr and returns a float, and that to_string takes an expr and returns a string. If you try to add a case that returns an int instead, the compiler will catch it instantly.
The function keyword combines a match with an implicit argument. It's concise and idiomatic.
What makes OCaml distinct
OCaml occupies an interesting space. It's functional but not purely functional like Haskell. You can use mutable state when you need it. It compiles to native code that runs fast, competitive with C in many benchmarks. And the type system is sound, meaning if your code compiles, entire classes of runtime errors are impossible.
The pattern matching is exhaustive too. If you add a new variant to your expr type and forget to handle it somewhere, the compiler warns you. This is why Jane Street trusts it for financial systems where bugs have real cost.
Trying OCaml in the browser
OneCompiler's OCaml environment is set up for exactly the kind of exploration that OCaml rewards:
- Define types, write functions, see the results. The cycle is fast.
- Pattern matching errors show up in compiler output so you can fix them immediately.
- Share your code with a link. Handy for code review discussions or teaching.
- No opam, no dune, no switch configuration. Just code.
If you're evaluating OCaml for the first time, this is the lowest-friction way to experience the type system. If you already know the language, it's a scratch pad for when you don't need a full project.