Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type 'a file = { mutable debut : 'a list; mutable fin : 'a list};;
- (* Complexité : O(1) *)
- let file_vide () = { debut = []; fin = []};;
- (* Complexité : O(1) *)
- let est_vide {debut = d; fin = f} = match (d, f) with
- | [], [] -> true
- | l1, l2 -> false;;
- (* Complexité : O(l) *)
- let miroir l =
- let rec miroir_aux l acc = match l with
- | [] -> acc
- | t::q -> miroir_aux q (t::acc)
- in miroir_aux l [];;
- (* Complexité : O(1) ou O(l) *)
- let premier {debut = d; fin = f} = match (d, f) with
- | [], [] -> failwith("Mauvaise file.")
- | [], l -> hd (miroir l)
- | t::q, l -> t;;
- (* Complexité : O(1) *)
- let enfile {debut = d; fin = f} elt = {debut = d; fin = elt::f};;
- (* Complexité : O(1) ou O(l) *)
- let defile f = match (f.debut, f.fin) with
- | [], [] -> f
- | [], l -> f.debut <- tl(miroir f.fin); f
- | t::q, l -> f.debut <- q; f.fin <- l; f;;
- let rec al liste = match liste with
- | [] -> ()
- | t::q -> print_int t; al q;;
- (* EXERCICE 2 *)
- type 'a pile = Nil | P of 'a * 'a pile;;
- (*let pile_vide () = Nil;;
- let est_vide p = match p with
- | Nil -> true
- | P(a, p2) -> false;;
- let miroir pile =
- let rec miroir_aux stack acc = match stack with
- | Nil -> acc
- | P(a, p2) -> P(a, (miroir_aux acc p2 ))
- in miroir_aux pile Nil;;
- let premier stack = match stack with
- | Nil -> failwith("Aucun")
- | P(a, p2) -> a;;
- let empiler stack elt = P(elt, stack);;
- let depiler stack = match stack with
- | Nil -> Nil
- | P(a, Nil) -> Nil
- | P(a, p2) -> p2;;
- let rec ap stack = match stack with
- | Nil -> ()
- | P(a, p2) -> print_int a; ap p2;;
- *)
- type couleur = Bleue | Rouge and assiette == couleur * int;;
- type 'a enreg = { mutable sommet : 'a; mutable queue : 'a pile }
- and 'a pile = Vide | P of 'a enreg;;
- let trier (assiettes:assiette pile) =
- let rouges = Vide in
- let bleues = Vide in
- while not est_vide assiettes do
- match assiettes.sommet with
- | (Bleue, a) -> empiler rouges (Bleue, a)
- | (Rouge, a) -> empiler bleues (Rouge, a);
- depiler assiettes;
- done;
- while not est_vide bleues do
- empiler assiettes (depiler bleues)
- done;
- while not est_vide rouges do
- empiler assiettes (depiler rouges)
- done;;
- (* EXERCICE 3 *)
- type 'a file = {mutable suivant: int; mutable debut: int; donnees: 'a vect; mutable est_vide: bool};;
- (* Est vide est nécessaire car sinon on peut confondre le cas file pleine et file vide.*)
- let est_vide file = file.est_vide;;
- let file_vide size x = {suivant = 0; debut = 0; donnees = make_vect size x; est_vide = true};;
- let premier LFEQ = if LFEQ.est_vide then failwith("Vide") else LFEQ.donnees.(LFEQ.debut);;
- let defile lafileenquestion =
- if lafileenquestion.est_vide then ()
- else lafileenquestion.debut <- (lafileenquestion.debut + 1) mod (vect_length lafileenquestion.donnees);;
- let enfile LFEQ x =
- if (not LFEQ.est_vide) &&
- LFEQ.suivant == LFEQ.debut
- then
- failwith("Dépassement de capacité.")
- else (
- print_int (LFEQ.suivant); print_string " / "; print_int LFEQ.debut; print_string("\n");
- LFEQ.est_vide <- false;
- LFEQ.donnees.(LFEQ.suivant) <- x;
- LFEQ.suivant <- (LFEQ.suivant + 1) mod (vect_length LFEQ.donnees);
- (*print_int LFEQ.suivant; print_string " / "; print_int LFEQ.debut; print_string "\n";
- *)());;
- let test = (file_vide 4 0);;
- test;;
- enfile test 12;;
- enfile test 13;;
- enfile test 15;;
- enfile test 15;; enfile test 153;;
- let print_file LFEQ =
- for i = 0 to vect_length LFEQ.donnees - 1 do
- print_int LFEQ.donnees.(i);
- print_string("\n")
- done;;
- print_file test;;
Advertisement
Add Comment
Please, Sign In to add comment