Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 3.70 KB | None | 0 0
  1. (* Les Listes *)
  2.  
  3. (* FONCTION estSingleton *)
  4. let estSingleton l =
  5.     match l with
  6.     |[]->false
  7.     |_::[]->true
  8.     |_::_->false
  9. ;;
  10.  
  11. (* TEST DE estSingleton *)
  12. let test_estSingleton =
  13.     (estSingleton (1::[]) = true) &&
  14.     (estSingleton [] = false) &&
  15.     (estSingleton (1::2::[]) = false)
  16. ;;
  17.  
  18. (* FONCTION deuxiemeElement *)
  19. let deuxiemeElement l =
  20.     match l with
  21.     |[]-> failwith "liste trop courte"
  22.     |_::[]-> failwith "liste trop courte"
  23.     |_::h::_-> h
  24. ;;
  25.  
  26. (* TEST DE deuxiemeElement *)
  27. let test_deuxiemeElement =
  28.     deuxiemeElement (1::2::[]) = 2
  29. ;;
  30.    
  31. (* FOCNTION mult5 *)
  32. let rec mult5 l =
  33.     match l with
  34.     |[]->0
  35.     |h::t-> if h mod 5 = 0
  36.             then 1 + mult5 t
  37.             else mult5 t
  38. ;;
  39.  
  40. (* TEST DE mult5 *)
  41. let test_mult5 =
  42.     mult5 (2::3::10::4::2::8::5::[]) = 2
  43. ;;
  44.  
  45. (* FONCTION addLast *)
  46. let rec addLast l n =
  47.     match l with
  48.     |[]->n::[]
  49.     |h::t->h::(addLast t n)
  50. ;;
  51.  
  52. (* TEST DE addLast *)
  53. let test_addLast =
  54.     addLast (1::2::3::4::[]) 5 = (1::2::3::4::5::[])
  55. ;;
  56.  
  57. (* FOCNTION remLast *)
  58. let rec remLast l =
  59.     match l with
  60.     |[]->[]
  61.     |h::t->if t = []
  62.         then
  63.             []
  64.         else
  65.             h::(remLast t)
  66. ;;
  67.  
  68. (* TEST DE remLast *)
  69. let test_remLast =
  70.     remLast (1::2::3::4::5::[]) = (1::2::3::4::[])
  71. ;;
  72.  
  73. (* FONCTION croissante *)
  74. let rec croissante l =
  75.     match l with
  76.     |[]-> true
  77.     |h::[]-> true
  78.     |h1::h2::[]-> h1<h2
  79.     |h1::h2::t-> h1<h2 && croissante t
  80. ;;
  81.  
  82. (* TEST DE croissante *)
  83. let test_croissante =
  84.     croissante (1::2::3::4::5::6::7::8::9::10::[])
  85. ;;
  86.  
  87. (* FONCTION membre *)
  88. let rec membre x l =
  89.     match l with
  90.     |[]-> false
  91.     |h::t-> h=x || membre x t
  92. ;;
  93.  
  94. (* TEST DE membre *)
  95. let test_membre =
  96.     membre 3 (1::2::3::4::[])
  97. ;;
  98.  
  99. (* FOCNTION cherche_i*)
  100. let rec cherche_i i l =
  101.     match l with
  102.     |[]-> failwith "liste trop courte"
  103.     |h::t-> if i=0
  104.                 then
  105.                     h
  106.                 else
  107.                     cherche_i (i-1) t
  108. ;;
  109.  
  110. (* TEST DE cherche_i *)
  111. let test_cherche_i =
  112.     cherche_i 3 (1::2::3::4::5::6::7::8::9::10::[]) = 4
  113. ;;
  114.  
  115. (* FONCTION insert *)
  116. let rec insert x i l =
  117.     match l with
  118.     |[]-> if i = 0
  119.             then
  120.                 x::[]
  121.             else
  122.                 failwith "liste trop courte"
  123.     |h::t-> if i=0
  124.                 then
  125.                     x::l
  126.                 else
  127.                     h::(insert x (i-1) t)
  128. ;;
  129.  
  130. (* TEST DE insert *)
  131. let test_insert =
  132.     insert 1 1 (3::3::3::3::3::[]) = (3::1::3::3::3::3::[])
  133. ;;
  134.  
  135. (* FONCTION supprime_i *)
  136. let rec supprime_i i l =
  137.     match l with
  138.     |[]-> failwith "liste trop courte"
  139.     |h::t-> if i=0
  140.                 then
  141.                     t
  142.                 else
  143.                     h::(supprime_i (i-1) t)
  144. ;;
  145.  
  146. (* TEST DE supprime_i *)
  147. let test_supprime_i =
  148.     supprime_i 2 (1::2::3::4::5::[]) = (1::2::4::5::[]);;
  149. ;;
  150.  
  151. (* FONCTION supprime *)
  152. let rec supprime x l =
  153.     match l with
  154.     |[]-> failwith "liste trop courte"
  155.     |h::t-> if h = x
  156.                 then
  157.                     t
  158.                 else
  159.                     h::(supprime x t)
  160. ;;
  161.  
  162. (* TEST DE supprime *)
  163. let test_supprime =
  164.     supprime 4 (1::2::3::4::5::6::7::[]) =(1::2::3::5::6::7::[])
  165. ;;
  166.  
  167. (* FONCTION supprimeMult5 *)
  168. let rec supprimeMult5 l =
  169.     match l with
  170.     |[]-> []
  171.     |h::t-> if h mod 5 = 0
  172.                 then
  173.                     supprimeMult5 t
  174.                 else
  175.                     h::(supprimeMult5 t)
  176. ;;
  177.  
  178. (* TEST DE supprimeMult5 *)
  179. let test_supprimeMult5 =
  180.     supprimeMult5 (1::2::3::4::5::6::7::8::9::10::[]) = (1::2::3::4::6::7::8::9::[])
  181. ;;
  182.  
  183. (* FONCTION supprimeP *)
  184. let propriteteP x = x mod 3 = 0;;
  185. let rec suppr_listeP l p =
  186.     match l with
  187.     |[]-> []
  188.     |h::t-> if (p h)
  189.                 then
  190.                     suppr_listeP t p
  191.                 else
  192.                     h::(suppr_listeP t p)
  193. ;;
  194.  
  195. (* TEST DE supprimeP*)
  196. let test_supprime =
  197.     suppr_listeP (1::2::3::4::5::6::7::8::9::10::[]) propriteteP = (1::2::4::5::7::8::10::[])
  198. ;;
  199.  
  200. (* FONCTION concat *)
  201. let rec concat l1 l2 =
  202.     match l1 with
  203.     |[]->l2
  204.     |h1::t1->h1::(concat t1 l2)
  205. ;;
  206.  
  207. (* TEST DE concat *)
  208. let test_concat =
  209.     concat (1::2::3::4::[]) (5::6::7::8::9::[]) = (1::2::3::4::5::6::7::8::9::[])
  210. ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement