datastructs.ml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. let intervertir file =
  2. let a = Stack.pop file and b = Stack.pop file in
  3. Stack.push b file;
  4. Stack.push a file
  5. ;;
  6. let p = Stack.create();;
  7. Stack.push 4 p;;
  8. Stack.push 3 p;;
  9. Stack.pop p;;
  10. Stack.length p;;
  11. let copie p =
  12. let t = Stack.create() and q = Stack.create() in
  13. while not (Stack.is_empty p) do
  14. let aux = Stack.pop p in
  15. Stack.push aux t;
  16. done;
  17. while not (Stack.is_empty t) do
  18. let aux = Stack.pop t in
  19. Stack.push aux p;
  20. Stack.push aux q;
  21. done;
  22. q
  23. ;;
  24. let rotation p =
  25. let q = Stack.create() and a = Stack.pop p in
  26. while not (Stack.is_empty p) do
  27. Stack.push (Stack.pop q) p
  28. done;
  29. Stack.push a p;
  30. while not (Stack.is_empty q) do
  31. Stack.push (Stack.pop q) p
  32. done;
  33. ;;
  34. type coord = float * float;;
  35. type couleur = Bleu;;
  36. Bleu;;
  37. type logarithme = Ln of float;;
  38. let evaluation x = let Ln a = x in log a;;
  39. evaluation (Ln 5.0);;
  40. type couleur2 = Bleu | Rouge | Jaune | Vert;;
  41. Vert;;
  42. type complex = Cartesien of float * float| Polaire of float * float;;
  43. let i = Cartesien (0., 1.);;
  44. let modulus z = match z with
  45. | Cartesien (x,y)-> sqrt(x**2. +. y**2. )
  46. | Polaire (r,t)-> r
  47. ;;
  48. type nat = Zero | S of nat;;
  49. let iszero n = match n with
  50. | Zero -> true
  51. | _ -> false;;
  52. let quatre = S(S(S(S(Zero))));;
  53. let rec entier n = match n with
  54. | Zero -> 0
  55. | S m -> 1+ entier m
  56. ;;
  57. let rec peano n = match n with
  58. | 0 -> Zero
  59. | _ -> S(peano (n-1));;
  60. peano 6;;
  61. let rec addition a b = match b with
  62. | Zero -> a
  63. | S m -> addition (S a) m;;
  64. addition (peano 5) (peano 6);;
  65. let rec multiplication m n = match m with
  66. | Zero -> Zero
  67. | S l -> addition n (multiplication l n);;
  68. multiplication (peano 2) (peano 2);;
  69. (* Vendredi 14 avril *)
  70. type 'a maList = Vide | Ajout of 'a*'a maList;;
  71. let liste2 = Ajout(1,Ajout(2,Ajout(3,Vide)));;
  72. let rec tete lister = match lister with
  73. | Vide -> 0
  74. | Ajout (m,q) -> m;;
  75. let rec queue lister = match lister with
  76. | Vide -> Vide
  77. | Ajout (m,q) -> q;;
  78. let rec longueur lister = match lister with
  79. | Vide -> 0
  80. | Ajout (m,q) -> 1 + longueur q;;
  81. type entier_gauss = {real:int; ima:int}
  82. let i = {real=0; ima=1}
  83. let addition_gauss n1 n2 =
  84. let result = {real= n1.real + n2.real; ima= n1.ima + n2.ima}in
  85. result;;
  86. let multiplication n1 n2 =
  87. let real1 = n1.real * n2.real - n1.ima * n2.ima and im1 = n1.real * n2.ima + n1.ima * n2.real in
  88. let result = {real = real1; ima = im1} in
  89. result;;
  90. let conjugaison n1 =
  91. let result = {real = n1.real; ima = -1 * n1.ima} in
  92. result;;
  93. type 'a pile = PileVide | Ajout of 'a * 'a pile;;
  94. let creer_pile_vide ()= let x = PileVide in
  95. x;;
  96. let etre_pile_vide p = match p with
  97. | PileVide -> true
  98. | _ -> false;;
  99. let empiler y p = let result = Ajout(y,p) in
  100. result;;
  101. let depiler p = match p with
  102. | PileVide -> failwith "PileVide"
  103. | Ajout(t,q) -> q;;
  104. let sommet p = match p with
  105. | PileVide -> failwith "PileVide"
  106. | Ajout(t,q) -> t;;
  107. type 'a pile3 = 'a list;;
  108. let creer_pile_vide2 () = ([]:'a pile3);;
  109. let pile_vide2 (p:'a pile3) = match p with
  110. | [] -> true
  111. | _ -> false;;
  112. let pile_empiler2 y (p:'a pile3) = (y::p : 'a pile3);;
  113. let depiler2 (p: 'a pile3) = match p with
  114. | [] -> failwith"ntm"
  115. | t::q -> (q : 'a pile3);;
  116. let sommet (p: 'a pile3) = match p with
  117. | [] -> failwith"ntm"
  118. | t::q -> t;;
  119. type 'a pile4 = 'a list ref;;
  120. let creer_pile_vide3 () = (ref []:'a pile4);;
  121. let pile_vide2 (p:'a pile4) = match !p with
  122. | [] -> true
  123. | _ -> false;;
  124. let pile_empiler2 y (p:'a pile4) = p:=y::!p;;
  125. let depiler2 (p: 'a pile4) = match !p with
  126. | [] -> failwith"ntm"
  127. | t::q -> p:=q;t
  128. ;;
  129. let sommet (p: 'a pile4) = match !p with
  130. | [] -> failwith"ntm"
  131. | t::q -> t;;
  132. type 'a pile5 = {mutable contenu : 'a list}
  133. let creer_pile_vide5 () = let x = {contenu = []} in
  134. x
  135. ;;
  136. let pile_vide5 (p:'a pile5) = match p.contenu with
  137. | [] -> true
  138. | _ -> false;;
  139. let pile_empiler5 y (p:'a pile5) = p.contenu <- y::p.contenu;;
  140. let depiler5 (p: 'a pile5) = match p.contenu with
  141. | [] -> failwith"ntm"
  142. | t::q -> p.contenu <- q;t
  143. ;;
  144. let rec insere_pile5 x (p : 'a pile5) = match (p.contenu : 'a list) with
  145. | [] -> pile_empiler5 x p; p
  146. | t::q when x<=t -> pile_empiler5 x (p : 'a pile5);p
  147. | t::q -> p.contenu <- q; pile_empiler5 t (insere_pile5 x p);p
  148. ;;
  149. let rec tri_inser_pile5 p = match p.contenu with
  150. | [] -> p
  151. | t::q -> p.contenu <- q; insere_pile5 t (tri_inser_pile5 p);;
  152. let pileatrier = {contenu = [5;3;8;90;34]};;
  153. tri_inser_pile5 pileatrier;;
  154. type symb = N of int | Op_binaire of (int -> int -> int) | Op_unaire of (int-> int);;
  155. type expr = symb list;;
  156. let machin = [N 3;N 10;N 5; Op_binaire (+) ; Op_binaire ( * )];;
  157. let rec aux p e = match p,e with
  158. | [n],[] -> n
  159. | t::q, Op_unaire f::r ->
  160. aux (f t)::q r
  161. |t1::t2::q, Op_binaire f::r ->
  162. aux (f t1 t2)::q r
  163. | _, N n::r -> aux (n::p) r;;
  164. let eval e = aux [] e;;
  165. type arbre = F of int | Nun of symb * arbre | Nlin of symb * arbre*arbre;;
  166. let explarbre = Nlin (Op_binaire ( * ), Nlin(Op_binaire ( + ), F 10, F 5), F 3)
  167. let rec eval2 a = match a with
  168. | F x -> x
  169. | Nun(Op_unaire f, x) -> f (eval2 x)
  170. | Nlin(Op_binaire f, x, y) -> f (eval2 x) (eval2 y);;