let intervertir file = let a = Stack.pop file and b = Stack.pop file in Stack.push b file; Stack.push a file ;; let p = Stack.create();; Stack.push 4 p;; Stack.push 3 p;; Stack.pop p;; Stack.length p;; let copie p = let t = Stack.create() and q = Stack.create() in while not (Stack.is_empty p) do let aux = Stack.pop p in Stack.push aux t; done; while not (Stack.is_empty t) do let aux = Stack.pop t in Stack.push aux p; Stack.push aux q; done; q ;; let rotation p = let q = Stack.create() and a = Stack.pop p in while not (Stack.is_empty p) do Stack.push (Stack.pop q) p done; Stack.push a p; while not (Stack.is_empty q) do Stack.push (Stack.pop q) p done; ;; type coord = float * float;; type couleur = Bleu;; Bleu;; type logarithme = Ln of float;; let evaluation x = let Ln a = x in log a;; evaluation (Ln 5.0);; type couleur2 = Bleu | Rouge | Jaune | Vert;; Vert;; type complex = Cartesien of float * float| Polaire of float * float;; let i = Cartesien (0., 1.);; let modulus z = match z with | Cartesien (x,y)-> sqrt(x**2. +. y**2. ) | Polaire (r,t)-> r ;; type nat = Zero | S of nat;; let iszero n = match n with | Zero -> true | _ -> false;; let quatre = S(S(S(S(Zero))));; let rec entier n = match n with | Zero -> 0 | S m -> 1+ entier m ;; let rec peano n = match n with | 0 -> Zero | _ -> S(peano (n-1));; peano 6;; let rec addition a b = match b with | Zero -> a | S m -> addition (S a) m;; addition (peano 5) (peano 6);; let rec multiplication m n = match m with | Zero -> Zero | S l -> addition n (multiplication l n);; multiplication (peano 2) (peano 2);; (* Vendredi 14 avril *) type 'a maList = Vide | Ajout of 'a*'a maList;; let liste2 = Ajout(1,Ajout(2,Ajout(3,Vide)));; let rec tete lister = match lister with | Vide -> 0 | Ajout (m,q) -> m;; let rec queue lister = match lister with | Vide -> Vide | Ajout (m,q) -> q;; let rec longueur lister = match lister with | Vide -> 0 | Ajout (m,q) -> 1 + longueur q;; type entier_gauss = {real:int; ima:int} let i = {real=0; ima=1} let addition_gauss n1 n2 = let result = {real= n1.real + n2.real; ima= n1.ima + n2.ima}in result;; let multiplication n1 n2 = let real1 = n1.real * n2.real - n1.ima * n2.ima and im1 = n1.real * n2.ima + n1.ima * n2.real in let result = {real = real1; ima = im1} in result;; let conjugaison n1 = let result = {real = n1.real; ima = -1 * n1.ima} in result;; type 'a pile = PileVide | Ajout of 'a * 'a pile;; let creer_pile_vide ()= let x = PileVide in x;; let etre_pile_vide p = match p with | PileVide -> true | _ -> false;; let empiler y p = let result = Ajout(y,p) in result;; let depiler p = match p with | PileVide -> failwith "PileVide" | Ajout(t,q) -> q;; let sommet p = match p with | PileVide -> failwith "PileVide" | Ajout(t,q) -> t;; type 'a pile3 = 'a list;; let creer_pile_vide2 () = ([]:'a pile3);; let pile_vide2 (p:'a pile3) = match p with | [] -> true | _ -> false;; let pile_empiler2 y (p:'a pile3) = (y::p : 'a pile3);; let depiler2 (p: 'a pile3) = match p with | [] -> failwith"ntm" | t::q -> (q : 'a pile3);; let sommet (p: 'a pile3) = match p with | [] -> failwith"ntm" | t::q -> t;; type 'a pile4 = 'a list ref;; let creer_pile_vide3 () = (ref []:'a pile4);; let pile_vide2 (p:'a pile4) = match !p with | [] -> true | _ -> false;; let pile_empiler2 y (p:'a pile4) = p:=y::!p;; let depiler2 (p: 'a pile4) = match !p with | [] -> failwith"ntm" | t::q -> p:=q;t ;; let sommet (p: 'a pile4) = match !p with | [] -> failwith"ntm" | t::q -> t;; type 'a pile5 = {mutable contenu : 'a list} let creer_pile_vide5 () = let x = {contenu = []} in x ;; let pile_vide5 (p:'a pile5) = match p.contenu with | [] -> true | _ -> false;; let pile_empiler5 y (p:'a pile5) = p.contenu <- y::p.contenu;; let depiler5 (p: 'a pile5) = match p.contenu with | [] -> failwith"ntm" | t::q -> p.contenu <- q;t ;; let rec insere_pile5 x (p : 'a pile5) = match (p.contenu : 'a list) with | [] -> pile_empiler5 x p; p | t::q when x<=t -> pile_empiler5 x (p : 'a pile5);p | t::q -> p.contenu <- q; pile_empiler5 t (insere_pile5 x p);p ;; let rec tri_inser_pile5 p = match p.contenu with | [] -> p | t::q -> p.contenu <- q; insere_pile5 t (tri_inser_pile5 p);; let pileatrier = {contenu = [5;3;8;90;34]};; tri_inser_pile5 pileatrier;; type symb = N of int | Op_binaire of (int -> int -> int) | Op_unaire of (int-> int);; type expr = symb list;; let machin = [N 3;N 10;N 5; Op_binaire (+) ; Op_binaire ( * )];; let rec aux p e = match p,e with | [n],[] -> n | t::q, Op_unaire f::r -> aux (f t)::q r |t1::t2::q, Op_binaire f::r -> aux (f t1 t2)::q r | _, N n::r -> aux (n::p) r;; let eval e = aux [] e;; type arbre = F of int | Nun of symb * arbre | Nlin of symb * arbre*arbre;; let explarbre = Nlin (Op_binaire ( * ), Nlin(Op_binaire ( + ), F 10, F 5), F 3) let rec eval2 a = match a with | F x -> x | Nun(Op_unaire f, x) -> f (eval2 x) | Nlin(Op_binaire f, x, y) -> f (eval2 x) (eval2 y);;