??

Hello OCaml

File: hello.ml

let bindings type inference pipe operator pattern matching Printf

Overview

Core OCaml building blocks — let bindings, type inference, Printf formatting, the pipe operator, and pattern matching on tuples. This is the best starting point if you're new to OCaml.

Key Concepts

Let Bindings & Type Inference

OCaml infers types — no annotations needed. Values are immutable by default:

let greeting = "Hello"
let name = "OCaml"
let version = 5
let full_greeting = greeting ^ ", " ^ name ^ "!"

Pipe Operator

Chain transformations left-to-right for readable data pipelines:

let result =
  [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
  |> List.filter (fun x -> x mod 2 = 0)
  |> List.map (fun x -> x * x)
  |> List.fold_left (+) 0

Pattern Matching on Tuples

let min_max lst =
  match lst with
  | [] -> None
  | x :: rest ->
    let rec aux lo hi = function
      | [] -> (lo, hi)
      | x :: xs -> aux (min lo x) (max hi x) xs
    in Some (aux x x rest)

Output

Hello, OCaml!
OCaml version 5
Area of circle (r=3.0): 28.2743
Sum of squares of evens 1..10: 220
Min: 1, Max: 9