Académique Documents
Professionnel Documents
Culture Documents
Cours 02
Michele Pagani
pagani@pps.univ-paris-diderot.fr
# Char . c o d e ’ a ’ ; ;
− : i n t = 97
# ’\097 ’;;
− : char = ’a ’
# ’\97 ’;;
^^
Error : I l l e g a l b a c ks l a s h escape i n s t r i n g or c h a r a c t e r (\9)
# Char . u p p e r c a s e ’ a ’ ; ;
− : c h a r = ’A ’
# Char . u p p e r c a s e ’[ ’;;
− : char = ’ [ ’
Un coup d’œil sur les modules
• Char.code appelle la fonction code du module Char.
• La bibliothèque standard de OCaml contient plusieurs modules
qu’on utilisera dans la suite: Char, String, List , Array, . . .
• pour appeler une fonction d’un module:
• soit on écrit le nom du module suivi du nom de la fonction:
# Char . c o d e ; ;
− : c h a r −> i n t = <fun>
• soit on ouvre le module avec le mot clé open et puis on appelle
ses fonctions librement
# code ; ;
^^^
E r r o r : Unbound v a l u e c o d e
# open Char ; ;
# code ; ;
− : c h a r −> i n t = <fun>
# " H e l l "^ ’o ’ ; ;
^^^
E r r o r : T h i s e x p r e s s i o n h a s t y p e c h a r b u t an
e x p r e s s i o n was e x p e c t e d o f t y p e s t r i n g
# l e t rec alphabet x =
l e t x _ s t r = S t r i n g . make 1 x i n
l e t x_nxt = Char . c h r ( Char . c o d e x +1) i n
i f x = ’ z ’ then x_str
e l s e x _ s t r ^( a l p h a b e t x_nxt ) ; ;
v a l a l p h a b e t : c h a r −> s t r i n g = <fun>
# alphabet ’a ’ ; ;
− : s t r i n g = " abcdefghijklmnopqrstuvwxyz "
# [ ’a ’; ’b’; ’c ’ ] ; ;
− : char l i s t = [ ’ a ’ ; ’b ’ ; ’c ’ ]
# [[1;2];[3]];;
− : int l i s t l i s t = [[1; 2]; [3]]
# [ ’a ’; ’b’; ’c ’ ] ; ;
− : char l i s t = [ ’ a ’ ; ’b ’ ; ’c ’ ]
# [[1;2];[3]];;
− : int l i s t l i s t = [[1; 2]; [3]]
# 1::2::3::[];; (∗ a s s o c i e a d r o i t e ∗)
− : int l i s t = [ 1 ; 2; 3]
# 1::2::3;;
^^
E r r o r : T h i s e x p r e s s i o n h a s t y p e i n t b u t an
e x p r e s s i o n was e x p e c t e d o f t y p e i n t l i s t
constructors de list
Une liste est soit vide soit a une tête et une queue
liste vide [] a un type polymorphe (⇒ plus tard)
# [];;
− : ’a l i s t = []
# 1::2::3::[];; (∗ a s s o c i e a d r o i t e ∗)
− : int l i s t = [ 1 ; 2; 3]
# 1::2::3;;
^^
E r r o r : T h i s e x p r e s s i o n h a s t y p e i n t b u t an
e x p r e s s i o n was e x p e c t e d o f t y p e i n t l i s t
constructors de list
Une liste est soit vide soit a une tête et une queue
liste vide [] a un type polymorphe (⇒ plus tard)
# [];;
− : ’a l i s t = []
# 1::2::3::[];; (∗ a s s o c i e a d r o i t e ∗)
− : int l i s t = [ 1 ; 2; 3]
# 1::2::3;;
^^
E r r o r : T h i s e x p r e s s i o n h a s t y p e i n t b u t an
e x p r e s s i o n was e x p e c t e d o f t y p e i n t l i s t
destructors de list
la “déstructuration” des listes (extraction des éléments) se fait par
pattern-mathcing (⇒ plus tard)
# let f = function
[ ] −> " v i d e "
| t : : q −> " p a s ␣ v i d e " ; ;
v a l f : ’ a l i s t −> s t r i n g = <fun>
# l e t f x = match x w i t h
[ ] −> " v i d e "
| t : : q −> " p a s ␣ v i d e " ; ;
v a l f : ’ a l i s t −> s t r i n g = <fun>
# f [ ];;
− : string = " vide "
# f [1;2];;
− : s t r i n g = " pas ␣ v i d e "
# length [ 1 ; 2 ; 3 ] ; ;
− : int = 3
# length [ " toto " ; " tata " ; " tutu " ] ; ;
− : int = 3
list (exemples)
• on peut avoir plus cas que les deux constructors:
# let f = function
[ ] −> " v i d e "
| t : : [ ] −> " s i n g l e t o n "
| t : : ( t t : : q ) −> " au ␣ moins ␣ deux ␣ e l e m e n t s " ; ;
v a l f : ’ a l i s t −> s t r i n g = <fun>
# length [ 1 ; 2 ; 3 ] ; ;
− : int = 3
# length [ " toto " ; " tata " ; " tutu " ] ; ;
− : int = 3
list (quelques fonctions)
# L i s t . hd [ 1 ; 2 ; 3 ] ; ;
− : int = 1
# List . tl [1;2;3];;
− : int l i s t = [ 2 ; 3]
# List . tl 1;;
^^
E r r o r : T h i s e x p r e s s i o n h a s t y p e i n t b u t an e x p r e s s i o n
was e x p e c t e d o f t y p e ’ a l i s t
# L i s t . hd [ ] ; ; (∗ exception , pas e r r e u r ∗)
E x c e p t i o n : F a i l u r e " hd " .
# hd [ 1 ; 2 ; 3 ] ; ;
− : int = 1
# hd [ ] ; ;
E x c e p t i o n : F a i l u r e " hd " .
# L i s t . f o l d _ l e f t ( f u n x y −> x^y ) " ! " [ " H e l l o " ; " ␣ " ; " w o r l d " ] ; ;
− : s t r i n g = " ! Hello ␣ world "
list (itérateurs)
# L i s t . f o l d _ l e f t ( f u n x y −> x^y ) " ! " [ " H e l l o " ; " ␣ " ; " w o r l d " ] ; ;
− : s t r i n g = " ! Hello ␣ world "
list (itérateurs)
# L i s t . f o l d _ l e f t ( f u n x y −> x^y ) " ! " [ " H e l l o " ; " ␣ " ; " w o r l d " ] ; ;
− : s t r i n g = " ! Hello ␣ world "
Pattern matching
(Filtrage par motifs)
Pattern matching
• fr.: filtrage par motif
# l e t r e c map f l i s t = match l i s t w i t h
[ ] −> [ ]
| t : : q −> ( f t ) : : ( map f q ) ; ;
v a l map : ( ’ a −> ’ b ) −> ’ a l i s t −> ’ b l i s t = <fun>
# l e t r e c map f l i s t = match l i s t w i t h
[ ] −> [ ]
| t : : q −> ( f t ) : : ( map f q ) ; ;
v a l map : ( ’ a −> ’ b ) −> ’ a l i s t −> ’ b l i s t = <fun>
# l e t r e c map f l i s t = match l i s t w i t h
[ ] −> [ ]
| t : : q −> ( f t ) : : ( map f q ) ; ;
v a l map : ( ’ a −> ’ b ) −> ’ a l i s t −> ’ b l i s t = <fun>
( ∗ q u e l e s t l e t y p e de c e s deux f o n c t i o n s ? ∗ )
l e t hd l = match l w i t h
| [ ] −> f a i l w i t h " L i s t e ␣ v i d e ␣ d a n s ␣ hd " ( ∗ e x c e p t i o n ∗ )
| a : : _ −> a ; ;
l e t t l l = match l w i t h
| [ ] −> f a i l w i t h " L i s t e ␣ v i d e ␣ d a n s ␣ t l " ( ∗ e x c e p t i o n ∗ )
| _ : : f i n l i s t e −> f i n l i s t e ; ;
(∗ q u e l e s t l ’ e r r e u r ? ∗)
l e t l e n g t h l = match l w i t h
| [ ] −> 0 ;
| t : : q −> 1+ ( l e n g t h q ) ; ;
Pattern matching (pattern)
# l e t r e c map f l i s t = match l i s t w i t h
[ ] −> [ ]
| t : : q −> ( f t ) : : ( map f q ) ; ;
v a l map : ( ’ a −> ’ b ) −> ’ a l i s t −> ’ b l i s t = <fun>
# l e t r e c map f l i s t = match l i s t w i t h
[ ] −> [ ]
| t : : q −> ( f t ) : : ( map f q ) ; ;
v a l map : ( ’ a −> ’ b ) −> ’ a l i s t −> ’ b l i s t = <fun>
# l e t r e c map f l i s t = match l i s t w i t h
[ ] −> [ ]
| t : : q −> ( f t ) : : ( map f q ) ; ;
v a l map : ( ’ a −> ’ b ) −> ’ a l i s t −> ’ b l i s t = <fun>
# l e t r e c map f l i s t = match l i s t w i t h
[ ] −> [ ]
| t : : q −> ( f t ) : : ( map f q ) ; ;
v a l map : ( ’ a −> ’ b ) −> ’ a l i s t −> ’ b l i s t = <fun>
# l e t r e c map f l i s t = match l i s t w i t h
[ ] −> [ ]
| t : : q −> ( f t ) : : ( map f q ) ; ;
v a l map : ( ’ a −> ’ b ) −> ’ a l i s t −> ’ b l i s t = <fun>
# l e t r e c map f l i s t = match l i s t w i t h
[ ] −> [ ]
| t : : q −> ( f t ) : : ( map f q ) ; ;
v a l map : ( ’ a −> ’ b ) −> ’ a l i s t −> ’ b l i s t = <fun>
# l e t r e c map f l i s t = match l i s t w i t h
[ ] −> [ ]
| t : : q −> ( f t ) : : ( map f q ) ; ;
v a l map : ( ’ a −> ’ b ) −> ’ a l i s t −> ’ b l i s t = <fun>
# l e t r e c map f l i s t = match l i s t w i t h
[ ] −> [ ]
| t : : q −> ( f t ) : : ( map f q ) ; ;
v a l map : ( ’ a −> ’ b ) −> ’ a l i s t −> ’ b l i s t = <fun>
# l e t r e c e v e n _ l e n g t h l = match l w i t h
[ ] −> t r u e
| [_] −> f a l s e
| _ : : _ : : r e s t e −> e v e n _ l e n g t h r e s t e ; ;
v a l e v e n _ l e n g t h : ’ a l i s t −> b o o l = <fun>
# even_length [ 1 ; 2 ; 3 ] ; ;
− : bool = f a l s e
# even_length [ 1 ; 2 ; 3 ; 4 ] ; ;
− : bool = true
Pattern matching (exemples)
Les motifs doivent être linéaires:
# l e t r e c e v e n _ l e n g t h l = match l w i t h
[ ] −> t r u e
| [_] −> f a l s e
| t : : t : : r e s t e −> e v e n _ l e n g t h r e s t e ; ;
^^
E r r o r : V a r i a b l e t i s bound s e v e r a l t i m e s i n t h i s m a t c h i n g
# l e t r e c e v e n _ l e n g t h l = match l w i t h
[ ] −> t r u e
| [_] −> f a l s e
| _ : : _ : : r e s t e −> e v e n _ l e n g t h r e s t e ; ;
v a l e v e n _ l e n g t h : ’ a l i s t −> b o o l = <fun>
# even_length [ 1 ; 2 ; 3 ] ; ;
− : bool = f a l s e
# even_length [ 1 ; 2 ; 3 ; 4 ] ; ;
− : bool = true
Pattern matching (exemples)
# l e t r e c l e n g t h l i s t = match l i s t w i t h
[ ] −> 0
| [ t ] @q −> 1+ l e n g t h q ; ;
^^^
E r r o r : Syntax e r r o r
Pattern matching (exemples)
# fact 2;;
Stack o v e r f l o w during e v a l u a t i o n ( l o o p i n g r e c u r s i o n ? ) .
Pattern matching (exemples)
Warning 8 : t h i s p a t t e r n −m a t c h i n g i s not e x h a u s t i v e .
Here i s an e x a m p l e o f a v a l u e t h a t i s not matched :
[]
v a l hd : ’ a l i s t −> ’ a = <fun>
# hd [ ] ; ;
E x c e p t i o n : M a t c h _ f a i l u r e ( " // t o p l e v e l // " , 1 1 3 , −6).
Pattern matching (exemples)
(∗ q u e l e s t l ’ e r r e u r ? ∗)
# l e t r e c t r o u v e a l i s t = match l i s t w i t h
[ ] −> f a l s e
| a : : _−> t r u e
| b : : q −> t r o u v e a q ; ;
v a l t r o u v e : ’ a −> ’ b l i s t −> b o o l = <fun>
# trouve 1 [ 1 ; 2 ; 3 ] ; ;
− : bool = true
# t r o u v e 42 [ 1 ; 2 ; 3 ] ; ;
− : bool = true
(∗ q u e l e s t l ’ e r r e u r ? ∗)
# l e t r e c t r o u v e a l i s t = match l i s t w i t h
[ ] −> f a l s e
| a : : _−> t r u e
| b : : q −> t r o u v e a q ; ;
v a l t r o u v e : ’ a −> ’ b l i s t −> b o o l = <fun>
# trouve 1 [ 1 ; 2 ; 3 ] ; ;
− : bool = true
# t r o u v e 42 [ 1 ; 2 ; 3 ] ; ;
− : bool = true
(∗ l a f o n c t i o n t r o u v e c o r r i g e ∗)
# l e t r e c t r o u v e a l i s t = match l i s t w i t h
[ ] −> f a l s e
| b : : q −> i f b=a t h e n t r u e e l s e t r o u v e a q ; ;
v a l t r o u v e : ’ a −> ’ a l i s t −> b o o l = <fun>
# trouve 1 [ 1 ; 2 ; 3 ] ; ;
− : bool = true
# t r o u v e 42 [ 1 ; 2 ; 3 ] ; ;
− : bool = f a l s e
Pattern matching (exemples)
• patterns avec des alternatives
let r e c f i b n = match n w i t h
0 | 1 −> n
| n −> f i b ( n−1)+ f i b ( n − 2 ) ; ;
val f i b : i n t −> i n t = <fun>
# fib 10;;
− : i n t = 55
• patterns avec des conditions
l e t r e c t r o u v e a l i s t = match l i s t w i t h
[ ] −> f a l s e
| b : : q when b=a −> t r u e
| _ : : q −> t r o u v e a q ; ;
v a l t r o u v e : ’ a −> ’ a l i s t −> b o o l = <fun>
# trouve 1 [1;2;3];;
− : bool = true
# trouve 42 [ 1 ; 2 ; 3 ] ; ;
− : bool = false
Doggy bag
• types char et string
• concatenation string ^
• quelques fonctions dans modules Char, String
code, cur, lowercase, . . . , length, get, make, sub, . . .
• type list :
• type polymorphe ’a list
• constructeurs [] , :: / “déstructuration” par filtrage par motifs
• quelques fonctions:
• concaténation @
• dans module List :
hd, tl , length, map, fold_right , fold_left
• filtrage par motifs (pattern matching)
• distinction de cas / déstructuration donnée
• motifs
(identificateurs/constructeurs, liaison, linéarité, ordre
d’évaluation, exhaustivité)
• motifs avec alternatives, conditions
Doggy bag
• types char et string
• concatenation string ^
• quelques fonctions dans modules Char, String
code, cur, lowercase, . . . , length, get, make, sub, . . .
• type list :
• type polymorphe ’a list
• constructeurs [] , :: / “déstructuration” par filtrage par motifs
• quelques fonctions:
• concaténation @
• dans module List :
hd, tl , length, map, fold_right , fold_left
• filtrage par motifs (pattern matching)
• distinction de cas / déstructuration donnée
• motifs
(identificateurs/constructeurs, liaison, linéarité, ordre
d’évaluation, exhaustivité)
• motifs avec alternatives, conditions
Doggy bag
• types char et string
• concatenation string ^
• quelques fonctions dans modules Char, String
code, cur, lowercase, . . . , length, get, make, sub, . . .
• type list :
• type polymorphe ’a list
• constructeurs [] , :: / “déstructuration” par filtrage par motifs
• quelques fonctions:
• concaténation @
• dans module List :
hd, tl , length, map, fold_right , fold_left
• filtrage par motifs (pattern matching)
• distinction de cas / déstructuration donnée
• motifs
(identificateurs/constructeurs, liaison, linéarité, ordre
d’évaluation, exhaustivité)
• motifs avec alternatives, conditions
Eh bien, maintenant vous pouvez le
comprendre !
1 l e t rec qSort l i s t =
2 match l i s t w i t h
3 | [ ] −> [ ]
4 | p i v o t : : r e s t −>
5 s p l i t pivot [ ] [ ] rest
6 and s p l i t p i v o t l e f t r i g h t l i s t =
7 match l i s t w i t h
8 | [ ] −> ( q S o r t l e f t )@( p i v o t : : ( q S o r t r i g h t ) )
9 | hd : : t l −>
10 i f hd <= p i v o t t h e n s p l i t p i v o t ( hd : : l e f t ) r i g h t tl
11 e l s e s p l i t p i v o t l e f t ( hd : : r i g h t ) t l ; ;