Slides Ocaml 2
Slides Ocaml 2
Slides Ocaml 2
List Liste
Tuple N-uplet
Record Enregistrement
Variant Somme
samuel.hornus@inria.fr
http ://www.loria.fr/˜shornus/ocaml/
Rappels
Toute expression a un type et une valeur.
Liaison d’un nom à une valeur :
let nom 1 = expr 1 and ...
and nom k = expr k [in expr ]
Filtrage par motifs :
match valeur with
| motif 1 -> expr 1
| motif 2 -> expr 2 | ...
Fonctions :
[let nom =] function arg -> expr
[let nom =] fun a b c -> expr
let nom a b c = expr
Rappels
# [45;2325;67;890];;
- : int list = [45; 2325; 67; 890]
# [1;’a’];;
Error: This expression has type char but an expression was
expected of type int
# [];;
- : ’a list = []
Les listes : tête et queue
# let l = 10::[20;22;56];;
val l : int list = [10; 20; 22; 56]
# let a = 42 in a::l;;
- : int list = [42; 10; 20; 22; 56]
# let a = 42 in a::l;;
- : int list = [42; 10; 20; 22; 56]
tête queue
Les listes : tête et queue
# let l = 10::[20;22;56];;
val l : int list = [10; 20; 22; 56]
# let a = 42 in a::l;;
- : int list = [42; 10; 20; 22; 56]
tête queue
# trouveMin [’q’;’w’;’e’;’r’;’t’;’y’];;
- : char = ’e’
# trouveMin [567;456;345;678;123;890];;
- : int = 123
# min;;
- : ’a -> ’a -> ’a = <fun>
Les listes : exemple : search / replace
Remplacer les occurences d’un élément dans une liste :
let rec remplace a b l = match l with
| [] -> []
| x::r -> (if x = a then b else x)::
(remplace a b r)
val remplace : ’a -> ’a -> ’a list -> ’a list = <fun>
Les listes : exemple : search / replace
Remplacer les occurences d’un élément dans une liste :
let rec remplace a b l = match l with
| [] -> []
| x::r -> (if x = a then b else x)::
(remplace a b r)
val remplace : ’a -> ’a -> ’a list -> ’a list = <fun>
# remplace 10 11 [19;17;10;480;-50;-10;10;22];;
- : int list = [19;17;11;480;-50;-10;11;22]
# tri [9;0;8;1;7;2;6;3;5;4];;
- : int list = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9]
Les n-uplets (tuples)
# let personne = ("Anne", 1980, "Nancy");;
val personne : string * int * string = ("Anne", 1980, "Nancy")
# let (prenom, ddn, ville) = personne;;
val prenom : string = "Anne"
val ddn : int = 1980
val ville : string = "Nancy" déconstruction
# let ( ,ddn, ) = personne;;
val ddn : int = 1980
# let ( ,ddn) = personne;;
Error: This expression has type string * int * string but an
expression was expected of type ’a * ’b
Les n-uplets (tuples)
# let personne = ("Anne", 1980, "Nancy");;
val personne : string * int * string = ("Anne", 1980, "Nancy")
# let (prenom, ddn, ville) = personne;;
val prenom : string = "Anne"
val ddn : int = 1980
val ville : string = "Nancy" déconstruction
# let ( ,ddn, ) = personne;;
val ddn : int = 1980
# let ( ,ddn) = personne;;
Error: This expression has type string * int * string but an
expression was expected of type ’a * ’b
Plus simplement :
# let translate (dx, dy) (x, y) =
(x +. dx, y +. dy);;
val translate : float * float -> float * float -> float * float
Exemple : représenter un point du plan
On représente un point du plan par une paire float * float.
Plus simplement :
# let translate (dx, dy) (x, y) =
(x +. dx, y +. dy);;
val translate : float * float -> float * float -> float * float
# norme (3.,4.);;
- : float = 5.0
Exemple 2 : division entière
On peut utiliser un n-uplet pour renvoyer plusieurs
valeurs.
Ici : le quotient et le reste d’une division entière.
Exemple 2 : division entière
On peut utiliser un n-uplet pour renvoyer plusieurs
valeurs.
Ici : le quotient et le reste d’une division entière.
# div 7 23;;
- : int * int = (0, 7)
Les enregistrements (records)
Interlude
On peut déclarer un nouveau type comme suit :
type [paramètres] nom = nouveau type ;;
ce qui correspond à peu près, en C, à :
typedef nouveau type nom ;
Exemples simples :
# type entier = int;;
type entier = int
1. les enregistrements
2. les sommes
Les enregistrements (records)
Les enregistrements sont des n-uplets plus flexibles :
chaque élément est nommé.
# e.ddn.jour;;
- : int = 31
En C++ :
union Data {
int entier;
char car;
};
sizeof(Data) == 4 octets
Les types somme (variants)
En OCaml :
ou
En C++ :
type
union Data {
int entier; constructeur
char car;
};
sizeof(Data) == 4 octets
Les types somme (variants)
En OCaml :
ou
En C++ :
type
union Data {
int entier; constructeur
char car;
};
sizeof(Data) == 4 octets
Les types somme : syntaxe
type [param] nom = Constructeur 1 [of type 1 ]
| Constructeur 2 [of type 2 ]
| ...
| Constructeur n [of type n ]
Les types somme : syntaxe
type [param] nom = Constructeur 1 [of type 1 ]
| Constructeur 2 [of type 2 ]
| ...
| Constructeur n [of type n ]
Exemple 1 :
type nombre = Ent of int | Flot of float
# Ent 12;;
- : nombre = Ent 12
# Flot 12.0;;
- : nombre = Flot 12.
Les types somme : exemple 1 (suite)
# let ajoute a b = match a with
| Ent(x) -> (match b with
| Ent(y) -> Ent (x+y)
| Flot(y) -> Flot (float x +. y))
| Flot(a) -> (match b with
| Ent(b) -> Flot (a +. float b)
| Flot(b) -> Flot (a+.b));;
val ajoute : nombre -> nombre -> nombre = <fun>
Les types somme : exemple 1 (suite)
# let ajoute a b = match a with
| Ent(x) -> (match b with
| Ent(y) -> Ent (x+y)
| Flot(y) -> Flot (float x +. y))
| Flot(a) -> (match b with
| Ent(b) -> Flot (a +. float b)
| Flot(b) -> Flot (a+.b));;
val ajoute : nombre -> nombre -> nombre = <fun>
# maxInListe l;;
- : char = ’r’
Un cas particulier : les Enums
# eval ex1;;
- : int = 14
fold left
v0 [ x1 ; x2 ; x3 ; x4 ; x5 ; x6 ]
f
f
f
f
f
fold left f v0 l
Les tours de Hanoı̈
A B C
n−1