Show Menu
Cheatography

Ocaml Cheat Sheet (DRAFT) by

Memo Ocaml

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Type

unit
void
int
integer
float
bool
stdbool.h
char
'A'
string
"­hel­lo"
'a list
head :: tail [1;3;2]
'a array
[|5;7;8|]

N-uplet

t1...tn
type inféré
let tuple = (a,b,c,d)
let (a,b,c) = tuple
fst / snd

Reference, string, array

let x=ref 3
variable mutable
x := 4 ;
affect­ation
!x
lecture
s.[0]
lecture string
t.(0)
lecture tab
t.(0) <- x;
écriture tab
s1 ^ s2
concat­énation
❗ string = immuables
 

Enregi­str­ements

type record =
{ field1 : bool;
mutable field2 : int; }
let r = {field1= true ; field2 = 3;}
let r' = { r with field1­=false }
r.field2 <-4;
let {field­1=v­ar_­a;_}=r

Type somme

type enum =
|cst
|pair of string*int
let c = cstt
let c = Pair ("ba­r", 3)

Boucles

while ... do ...done;
❗creer variable mutable
for i=... to ... do ...done;
i innacc­essible
for i= .... dowto ... do ...done;
à l'envers

Exception

raise Invali­d_a­rgument "­bla­bla­"
"­bla­bla­"= mess
raise Divisi­on_­by_zero
try ...
with
|Divis­ion­_by­_zero -> ....
|Inval­id_­arg­ument mess->
mess est utilisable