(* ============================================== Cours 2 - Syntaxe OCaml : let ============================================== *) (* ------------------------------------------------- 1. let pour nommer des valeurs ------------------------------------------------- *) let pi = 3.14159 let nom = "OCaml" let reponse = 42 (* ------------------------------------------------- 2. let pour définir des fonctions ------------------------------------------------- *) (* Définition complète (sugar pour fun) : *) let double x = 2 * x (* Est strictement équivalente à : *) let double' = fun x -> 2 * x let carre x = x * x let pair x = x mod 2 = 0 let () = Printf.printf "double 21 = %d\n" (double 21); Printf.printf "carre 7 = %d\n" (carre 7); Printf.printf "pair 4 = %B\n" (pair 4) (* ------------------------------------------------- 3. Fonctions à plusieurs arguments ------------------------------------------------- *) let addition x y = x + y let multiplication x y = x * y (* Les deux écritures sont équivalentes : *) let addition1 = fun x -> fun y -> x + y let addition2 x y = x + y let () = Printf.printf "addition 3 5 = %d\n" (addition 3 5); Printf.printf "multiplication 4 7 = %d\n" (multiplication 4 7) (* ------------------------------------------------- 4. let...in : portée locale ------------------------------------------------- *) (* Calculer l'aire d'un disque *) let aire_disque r = let pi = 3.14159 in pi *. r *. r (* Chaînage de let...in *) let resultat = let x = 5 in let y = 6 in x * x + y * y let () = Printf.printf "aire_disque 2.0 = %f\n" (aire_disque 2.0); Printf.printf "5^2 + 6^2 = %d\n" resultat (* ------------------------------------------------- 5. let et fonctions d'ordre supérieur ------------------------------------------------- *) (* Une fonction qui prend une fonction en argument *) let applique f x = f x let () = Printf.printf "applique (fun x -> x*x) 5 = %d\n" (applique (fun x -> x * x) 5) (* Une fonction qui retourne une fonction *) let ajoute n = fun x -> x + n let ajoute_5 = ajoute 5 let () = Printf.printf "ajoute 5 10 = %d\n" (ajoute 5 10); Printf.printf "ajoute_5 10 = %d\n" (ajoute_5 10) (* Une fonction qui applique deux fois *) let deux_fois f x = f (f x) let () = Printf.printf "deux_fois (fun x -> x*x) 3 = %d\n" (deux_fois (fun x -> x * x) 3) (* Composition : f après g *) let compose f g x = f (g x) let () = let f = compose (fun x -> x * 2) (fun x -> x + 1) in Printf.printf "compose (x->2*x) (x->x+1) 5 = %d\n" (f 5) (* (5 + 1) * 2 = 12 *) (* ------------------------------------------------- 6. let et listes ------------------------------------------------- *) let lst = [1; 2; 3; 4; 5] (* Fonctions sur listes définies avec let *) let premiers n = List.filter (fun x -> x <= n) lst let doubles lst = List.map (fun x -> 2 * x) lst let somme lst = List.fold_left (+) 0 lst let () = Printf.printf "premiers 3 [1..5] = [%s]\n" (String.concat "; " (List.map string_of_int (premiers 3))); Printf.printf "doubles [1..5] = [%s]\n" (String.concat "; " (List.map string_of_int (doubles lst))); Printf.printf "somme [1..5] = %d\n" (somme lst)