Programmation fonctionnelle
Transcription
Programmation fonctionnelle
16/01/2017 Fonction C’est un type prédéfini modéliser les fonctions usuelles Programmation fonctionnelle • Les arguments • La valeur déduite des arguments Cours 2 : les fonctions Licence 1 Année 2016 – 2017 En math, deux façons de définir L’objet Les valeurs f : x → 3x+7 f(x) = 3x+7 f : (x,y) → | 3x+2 | 3y+5 Par N. VINCENT 1 Licence 1 - programmation fonctionnelle Interprétation let # let f x = x + 3 ;; val f : int -> int = <fun> Deux façons de définir en caml f : x → 3x+7 • val f(x) = 3x+7 # let f = fun x -> 3 * x + 7 ;; val f : int -> int = <fun> # let f x = 3 * x + 7 ;; val f : int -> int = <fun> La véritable fonction est # fun (funxx-> ->33**xx++77) ;; - : int -> int = <fun> # let f x = 3 * x + 7 ;; val f : int -> int = <fun> # f 6 ;; - : int = 25 4 Licen ce 1 - p ro g rammatio n fo nctio n n elle Appel d'une fonction # let f x = 3 * x + 7 ;; val f : int -> int = <fun> # f +6 ;; Error: This expression has type int -> int but an expression was expected of type int # f (-6) ;; - : int = -11 # f 3 + 5 ;; - : int = 21 # f -6 ;; Error: This expression has type int -> int but an expression was expected of type int # f (3 + 5) ;; - : int = 31 # f 6. ;; Error: This expression has type float but an expression was expected of type int Avec la véritable fonction # (fun x -> 3 * x + 7) 6 ;; - : int = 25 Licence 1 - programmation fonctionnelle • int ->int • Analyse de l’argument • Analyse du résultat • = <fun> Arguments séparés du nom par un espace Arguments séparés du nom par un espace # f(6) ;; - : int = 25 # let x = 3 ;; val x : int = 3 f 3 Licen ce 1 - p ro g rammatio n fo nctio n n elle Appel d'une fonction 2 Licence 1 - programmation fonctionnelle La définition d'une fonction Début de définition par le mot clé f(x,y) = (3x+2,3y+5) # f (2=2) ;; Error: This expression has type bool but an expression was expected of type int 5 Licence 1 - programmation fonctionnelle # f 2 = 2 ;; - : bool = false 6 1 16/01/2017 Arguments locaux à la fonction Remarques Le nom des arguments est local à la fonction # let x = 5 ;; val x : int = 5 • Nombre quelconque d’arguments # let f x = x + 3 ;; val f : int -> int = <fun> • Un seul résultat # f 6 ;; - : int = 9 • Arguments et résultat sont de tout type # f x ;; - : int = 8 même des fonctions # x ;; - : int = 5 7 Licence 1 - programmation fonctionnelle Licence 1 - programmation fonctionnelle Fonction et arguments Appel d'une fonction Arguments séparés par un espace Fonctions de plusieurs arguments # let f x y = 2 + x + y ;; val f : int -> int -> int = <fun> # let f x y = 2 + x + y ;; val f : int -> int -> int = <fun> x est l’antécédent y->2+x+y est le résultat # f 1 3 ;; - : int = 6 # let g = fun x -> (fun y -> 2 + x + y) ;; val g : int -> int -> int = <fun> # f 2 3 ;; - : int = 7 Licen ce 1 - p ro g rammatio n fo nctio n n elle - : int = 6 # f (1,3) ;; Error: This expression has type int * int but an expression was expected of type int 9 La nature des arguments Licen ce 1 - p ro g rammatio n fo nctio n n elle 10 La nature des arguments Il n'est pas nécessaire de mettre des parenthèses, # let f x y = 2 + x + y ;; le séparateur est l'espace val f : int -> int -> int = <fun> # f 10 3 ;; - : int = 15 Tous les arguments n’ont pas le même type # let ff x y = x + int_of_float y ;; val ff : int -> float -> int = <fun> # ff 2 6.4 ;; - : int = 8 # f 10 -3 ;; Error: This expression has type int -> int but an expression was expected of type int # ff 10 (-4.4) ;; - : int = 6 Calcul suivant la valeur d’un booléen # (10 -3) -3) ;;;; # ff (10 - : int -> int = <fun> # let f x test = if test then x+1 else x ;; val f : int -> bool -> int = <fun> # f 10 ;; - : int -> int = <fun> Licence 1 - programmation fonctionnelle # f (1) (3) ;; # f (1 3) ;; Error: This expression is not a function; it cannot be applied # g 2 3 ;; - : int = 7 # f 10 (-3) ;; - : int = 9 8 # f 4 true ;; - : int = 5 11 # let x = 6 ;; val x : int = 6 # f x (x mod 2 = 0) ;; - : int = 7 12 Licence 1 - programmation fonctionnelle 2 16/01/2017 Fonction de plusieurs variables # let f x y = 2 + x + y ;; val f : int -> int -> int = <fun> Nature de Fonction de plusieurs variables # let g = fun x -> (fun y -> 2 + x + y) ;; val f : int -> int -> int = <fun> Une fonction # let f x test = if test then x+1 else x ;; val f : int -> bool -> int = <fun> # f 10 #f 10;; - : int -> int = <fun> Nature de Une fonction # let ff = f 10 ;; #g 10;; val ff : int -> int = <fun> - : int -> int = <fun> # g 10 Nature de # f 10 z Error: Unbound value z # let h z = f 10 z ;; Val h : int -> int = <fun> # ff 2 ;; - : int = 14 # h 2 ;; - : int = 14 # let t = ff true ;; val t : int -> int = <fun> Licence 1 - programmation fonctionnelle 14 Construction de nouveaux types let f (x,y) Un constructeur pour chaque nouveau type # let f (x,y) = 2 * x + y ;; val f : int * int -> int = <fun> A partir d'un constructeur : la virgule (x,y) Les parenthèses encadrent le n-uplet mais ne sont pas toujours indispensables Les éléments du n-uplet ne sont pas nécessairement de même type Fonction avec un argument De type int*int Introduit un nouveau type : le type couple ou plus généralement n-uplet Possibilité d'agréger des types variés par constructeurs 15 Licen ce 1 - p ro g rammatio n fo nctio n n elle # 1 , "vrai" ;; - : int * string = (1, "vrai") # 2 , 1=1 ;; - : int * bool = (2, true) f x, y Licence 1 - programmation fonctionnelle # let g x y = fun x y -> x + y ;; val g : 'a -> 'b -> int -> int -> int = <fun> # let h (x,y) = x + y ;; val h : int * int -> int = <fun> • Attention à la position des parenthèses f(x,y) 16 # let f x y = x + y ;; val f : int -> int -> int = <fun> • La virgule est le constructeur # (2 , 1.2) ;; - : int * float = (2, 1.2) Licen ce 1 - p ro g rammatio n fo nctio n n elle Fonction avec plusieurs arguments Les n-uplets (2 , 1.2) (2 , 1=1) (1 , "vrai") val h : bool -> int = <fun> val hh : int -> int = <fun> Les ou l’argument # let f x y = 2 * x + y ;; val f : int -> int -> int = <fun> # let h z = f 10 z ;; # let hh x = f x true ;; 13 Licence 1 - programmation fonctionnelle # let ff test x = if test then x+1 else x ;; val ff : bool -> int -> int = <fun> 17 f – 2 arguments g – arguments h – 1 argument Licence 1 - programmation fonctionnelle 18 3 16/01/2017 # let f x y = x + y ;; # let g x y = fun x y -> x + y ;; # let h (x,y) = x + y ;; Les appels # f 3 2 ;; - : int = 5 # g 3 2 ;; - : int -> int -> int = <fun> # g 2 3 5 2 ;; - : int = 7 # g 4. true 5 2 ;; - : int = 7 Les points importants Les fonctions Ses arguments Les n-uplets # h 3 2 ;; Error: This function is applied to too many arguments; maybe you forgot a `;' h 3 , 2 ;; Error: This expression has type int but an expression was expected of type int * int # h (3,2) ;; - : int = 5 Licence 1 - programmation fonctionnelle 19 Licence 1 - programmation fonctionnelle 20 4