OCaml Functional language
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
let x = 1let f x = x + 1
match v with | ...
type t = A | B of int
dune builddune exec ./bin/main.exe
dune testutopConcepts
- opam switch + packages
- dune dune-project, dune files
- ocamlfind / ocamlopt native or bytecode
- Executable _build/default
Values & functions
let x = 42let x = 1 in x + 1
let add a b = a + b
let sq = fun x -> x * x
let rec fact n = if n <= 1 then 1 else n * fact (n -
1)
let add5 = add 5
f @@ g xx |> f |> g
let f ~name ?(n = 1) () = ...
let (a, b) = pair
let f = function | 0 -> "zero" | _ ->
"other"
let x = ref 0 in x := !x + 1
if c then a else b
for i = 0 to n - 1 do ... done
while c do ... done
print_endline "hi"
Printf.printf "%d %s\n" n s
Printf.sprintf "%.2f" x
Types & pattern matching
type t = Red | Green | Blue of int
type p = { name : string; age : int }
{ p with age = 31 }
type 'a tree = Leaf | Node of 'a tree * 'a *
'a tree
match v with | A -> 1 | B n -> n
| x :: rest -> ... | [] -> ...
| Some x -> x | None -> d
| n when n > 0 -> ...
| (1 | 2) as n -> ...
let p = { name = "Ann"; age = 30 }
p.agetype t = { mutable n : int }
type id = int`Tag 3exception Oops of string
try f () with Not_found -> d
raise (Failure "msg")
assert (x > 0)
Option & result
Some x / NoneOption.map f oOption.value o ~default:d
Option.bind o fOption.is_some o
Ok v / Error eResult.map f rResult.bind r flet* x = r in ...
Result.get_ok rLists & arrays
[1; 2; 3]x :: xsa @ bList.map f lList.filter p lList.fold_left f acc l
List.iter f lList.length lList.rev lList.mem x lList.find_opt p l
List.assoc_opt k l
List.sort compare l
List.init n fList.exists p l / List.for_all p l
List.nth l i[| 1; 2; 3 |]a.(i) / a.(i) <- v
Array.make n 0Array.length aArray.iter f a / Array.map f a
Strings & I/O
"ab" ^ "cd"
String.length sString.sub s pos len
String.split_on_char ',' s
String.concat ", " l
String.trim sString.uppercase_ascii s
string_of_int n / int_of_string s
float_of_int nString.equal a b
Buffer.add_string b s
read_line ()In_channel.with_open_text f In_channel.input_all
Out_channel.with_open_text f (fun oc -> ...)
Sys.argvModules
module M = struct ... end
module type S = sig ... end
module M : S = struct ... end
open Mlet open M in ...
M.(expr)module F (X : S) = struct ... end
module IntMap = Map.Make (Int)
module StrSet = Set.Make (String)
Hashtbl.create 16
Hashtbl.replace h k v
Hashtbl.find_opt h k
include Mfile.ml / file.mli
Dune
dune init project <name>
dune builddune build -wdune exec <name>
dune exec ./bin/main.exe -- <args>
dune testdune runtest --auto-promote
dune cleandune fmtdune utop <dir>
dune build @docdune build @check
dune installdune build --release
opam & toolchain
opam initopam install <pkg>
opam search <text>
opam update && opam upgrade
opam listopam switch create <name> <version>
opam switch list
opam switch <name>
eval $(opam env)
opam install --deps-only .
opam switch create . --deps-only
opam pin add <pkg> <path-or-url>
opam exec -- <cmd>
opam install ocaml-lsp-server ocamlformat utop
ocaml -versionToplevel & compiler
utopocamlocaml <file.ml>
#use "file.ml";;
#require "<lib>";;
#show <name>;;
ocamlfind ocamlopt -package <lib> -linkpkg f.ml
ocamlfind ocamlopt f.ml -o f
ocamlc -c f.mliocamlfind listRecipes
opam switch create . --deps-onlydune init project hellocd hellodune exec hello
# dune-project(lang dune 3.0)# bin/dune(executable (name main) (libraries str))
let%expect_test _ = print_endline "hi"; [%expect {| hi |}]# dune: (inline_tests) (preprocess (pps ppx_expect))
let sum l = List.fold_left ( + ) 0 llet () = [1; 2; 3] |> sum |> Printf.printf "%d\n"



