Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. (* *)
  2.  
  3. let rec list_map l f = match l with
  4. | [] -> []
  5. | t::q -> (f t)::(list_map q f);;
  6.  
  7. let rec list_select l p = match l with
  8. | [] -> [];
  9. | t::q -> if (p t) then t::(list_select q p) else (list_select q p);;
  10.  
  11. let rec insere l e = match l with
  12. | [] -> [];
  13. | t::q -> if e < t then e::(t::q) else t::(insere q e);;
  14.  
  15. let rec tri_insertion l = match l with
  16. | [] -> [];
  17. | t::q -> insere (tri_insertion q) t;;
  18.  
  19. let list_rev l =
  20. let rec aux z acc = match z with
  21. | [] -> acc;
  22. | t::q -> aux q (t::acc);
  23. in aux l [];;
  24.  
  25. (* Entiers et base *)
  26. let rec puissance x n = match n with
  27. | 0 -> n;
  28. | 1 -> x;
  29. | n -> x * (puissance x (n-1));;
  30.  
  31. let rec pgcd p q =
  32. if p = q then p
  33. else (
  34. let rec pgcd_aux a b =
  35. if (a mod b = 0) then -1
  36. else
  37. let p = pgcd_aux b (a mod b) in
  38. if p = -1 then a mod b else pgcd_aux b (a mod b);
  39. in max (pgcd_aux p q) (pgcd_aux q p);
  40. );;
  41.  
  42. let base x b =
  43. let retourne v =
  44. let r = (make_vect (vect_length v) 0) in
  45. for i = 0 to vect_length v - 1 do
  46. r.(i) <- v.((vect_length v) - 1 - i)
  47. done;
  48. r;
  49. in
  50. let rec aux y c v i =
  51. v.(i) <- (y mod b);
  52. if y >= c then aux (y / b) c v (i + 1);
  53.  
  54. in let v = (make_vect (int_of_float(floor (log(float_of_int x) /. log(float_of_int b))) + 1) 0)
  55. in (aux x b v 0) ; retourne v;;
  56.  
  57. base 12 10;;
  58.  
  59. type 'a vecteur = { mutable l:int; mutable i:int; mutable v: 'a vect };;
  60.  
  61. let ajouter_vecteur v e =
  62. if v.i = v.l - 1 then
  63. begin
  64. let r = (make_vect (2 * v.l) 0) in
  65. for j = 0 to v.l - 1 do
  66. r.(j) <- v.v.(j);
  67. done;
  68. v.v <- r;
  69. end;
  70. v.v.(v.i) <- e;
  71. v.i <- v.i + 1;;
  72.  
  73. let supprimer_vecteur v =
  74. if v.i > 0 then v.i <- v.i - 1 else failwith "Vecteur vide.";;
  75.  
  76. let monvecteur = { l = 5; i = 0; v = [|0;0;0;0;0|] } in ajouter_vecteur monvecteur 2;
  77. ajouter_vecteur monvecteur 3;
  78. ajouter_vecteur monvecteur 3;
  79. ajouter_vecteur monvecteur 3;
  80. ajouter_vecteur monvecteur 3;
  81. ajouter_vecteur monvecteur 3;
  82.  
  83. monvecteur.v;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement