Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
## Variables
```ocaml
let x_g = 3;;
let x = 2 in 3 + x * x;;
let x = 5 in
let y = x + 2 in
x * y;;
let x =
let y = 3
in y * y
in x + 10;;
```
## Appels de fonction
```ocaml
not false;;
log 10.0;;
abs (int_of_string "-3");;
hypot 3.0 4.0;;
hypot (2.5 +. 0.5) (2.0 *. 2.0);;
hypot 2.0 1.5 *. 2.0 ;;
hypot 2.0 (1.5 *. 2.0) ;;
```
## Définitions de fonction
```ocaml
let carre (x:int): int = x * x;;
let carre2 x = x * x;;
let cube x = x * x * x
in cube 2;;
let cube (x:int): int =
let carre3 y = y * y
in carre3 x * x;;
cube 4;;
carre3;;
```
## Énumérations
```ocaml
type jour =
| Lundi
| Mardi
| Mercredi
| Jeudi
| Vendredi
| Samedi
| Dimanche
;;
Lundi;;
Mercredi;;
Lundi = Mercredi;;
Lundi < Mercredi;;
```
## Filtrage
```ocaml
match Lundi with
| Mardi -> 2
| Mercredi -> 3
| Lundi -> 1
| Vendredi -> 5
| Dimanche -> 7
| Samedi -> 6
| Jeudi -> 4
;;
match Lundi with
| Mardi -> 2
| Mercredi -> 3
| Lundi -> 1
| Vendredi -> 5
| Dimanche -> 7
| Samedi -> 6
(* | Jeudi -> 4 *)
;;
let weekend (j: jour): bool =
match j with
| Samedi -> true
| Dimanche -> true
| autre -> false
;;
let weekend (j: jour): bool =
match j with
| Samedi -> true
| Dimanche -> true
| _ -> false
;;
```
## Nuplet
```ocaml
match (3.5, "coucou") with
| (_, s) -> s^" tout le monde"
;;
```
## Filtrage sur types de base
```ocaml
let is_zero (n: int): bool =
match n with
| 0 -> true
| _ -> false
;;
is_zero 1;;
is_zero 0;;
```
## Types sommes avec données
```ocaml
type nombre =
| Int of int
| Float of float
| Cplx of float * float
| Nan
;;
Int 3;;
Cplx (3.0, 7.2);;
let int_of_nombre (n: nombre): int =
match n with
| Int i -> i
| Float f -> int_of_float f
| Cplx (r,_) -> int_of_float r
| Nan -> 0
;;
int_of_nombre (Cplx (3.0, 7.2));;
```
## Pattern matching en profondeur
```ocaml
let is_zero (n: nombre): bool =
match n with
| Int 0 -> true
| Float 0.0 -> true
| Cplx (0.0, 0.0) -> true
| _ -> false
;;
```
## Rappel récursivité
```ocaml
(**
Fonction qui calcule la somme des carrés des n premiers entiers
@param n le nombre d'entiers
@return la somme
*)
let rec somme_carres (n: int): int =
if n <= 0
then 0
else n * n + somme_carres (n-1);;
```
## Listes
```ocaml
let l1 = 1 :: 3 :: 5 ::[] ;;
let l2 = [ 1; 3; 5 ];;
l1 = l2;;
type resultat = Aucun | Int of int;;
(**
Cette fonction donne le maximum d'un liste d'int
@param l la liste dont on veut le max
@return le max ou Aucun si la liste est vide
*)
let rec maximum (l: int list): resultat =
match l with
| [] -> Aucun
| x :: [] -> Int x
| x :: l2 ->
match maximum l2 with
| Aucun -> Int x
| Int y -> if x > y
then Int x
else Int y
;;
maximum [ 2; 1; 5; 3 ];;
maximum [ 2 ];;
maximum [];;
(**
Calcule le maximum dans une liste de liste d'int
@param ll la liste de liste
@return le max ou Aucun s'il n'y a pas d'int dans ll
*)
let rec maximum2 (ll: int list list): resultat =
match ll with
| [] -> Aucun
| l :: [] -> maximum l
| l :: ll2 ->
let ml = maximum l in
let mll2 = maximum2 ll2 in
match ml, mll2 with
| Aucun, m -> m
| m, Aucun -> m
| Int m1, Int m2 ->
if m1 < m2
then Int m2
else Int m1
;;
maximum2 [ [2; 3]; [4; 1]; [1; 6; 8]];;
maximum2 [ []; [1]; [0; 0]];;
maximum2 [ []; [] ];;
maximum2 [];;
```