Code · 1 file
Sample OCaml File
OCaml, a functional language with records, pattern matching and printf. Every language sample implements the same small program so you can compare them side by side.
OCaml · 608 B
Sample OCaml file
An idiomatic OCaml example — the same customer program (or, for hardware languages, a small counter design) we offer in every language.
CC0 · free for any use · Verified by an independent tool · Report a problem
(* Sample OCaml program — samplefiles.com *)
type customer = { id : int; name : string; balance : float }
let is_vip c = c.balance >= 10_000.
let total_balance customers = List.fold_left (fun acc c -> acc +. c.balance) 0. customers
let () =
let customers =
[ { id = 1; name = "Ada"; balance = 12_500. };
{ id = 2; name = "Grace"; balance = 830.5 };
{ id = 3; name = "Linus"; balance = 10_000. } ]
in
List.iter
(fun c -> Printf.printf "%-6s %10.2f %s\n" c.name c.balance (if is_vip c then "VIP" else ""))
customers;
Printf.printf "Total: %.2f\n" (total_balance customers)File details
Download from the command line
curl -L -o sample.ml https://files.samplefiles.com/code/ml/sample.mlwget https://files.samplefiles.com/code/ml/sample.mlhttps://files.samplefiles.com/code/ml/sample.mlprintf '%s %s\n' 131343184124c505d5bd974582e83a183c29e3fa2118e0af11c2b60f14051f4d sample.ml | shasum -a 256 -cOr check the download in your browser with the checksum calculator.