(******************************************************************************) (* 1. Premiers pas ************************************************************) (******************************************************************************) (* 1.1. Évaluation d'expressions **********************************************) 51;; 16 - 64;; 32 * (51+1);; 8 / 3;; 3.14;; 2 * 3.14;; 2. *. 3.14;; "Hello world!";; ("haha",3);; true;; true && false;; 2+2 = 4;; if 3*3 > 8 then "Bravo" else "Bouh";; if false || not true then "un" else 2;; 2 * (* ceci est un commentaire *) 3;; (* 1.2. Déclaration de variables : let et let ... in **************************) let a = 4;; a;; let b = 13 * a;; b;; let a = a * a;; a;; b;; let c = 2 in a * b * c;; c;; let a = 21 in a * 2;; a;; let (a,b) = (3,4) in a * b;; (* 1.3. Fonctions *************************************************************) let carre x = x * x;; carre 4;; let fois x y = x * y;; fois 3 4;; let fois_3 = fois 3;; fois_3 14;; let rec plus x y = if y = 0 then x else plus (x+1) (y-1);; plus 3 0;; plus 3 4;; plus 3 (-1);; (* boum ! rappelez-vous comment interrompre le calcul *) let rec f x = f x;; f 0;; (* re-boum ! *) (fun x -> x*x*x) 4;; let apply_rev f x y = f y x;; let g x y = (x,y);; apply_rev g 64 16;; apply_rev (fun x y -> x / y) 3 18;; let rec f x = (x x);; (* 1.4. Listes ****************************************************************) let est_vide l = (l = []);; let rec produit l = match l with | [] -> 1 | x::xs -> x * (produit xs);; produit [2;8;4;2;13];; (******************************************************************************) (* 2. À vous maintenant ! *****************************************************) (******************************************************************************) (* 2.2. Manipulation de listes ************************************************) let div x y = if y = 0 then failwith "Division par zéro !" else x / y;; div 9 3;; div 9 0;;