Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. (* Chapitre 2 *)
  2. (* Exercice 3 *)
  3. type 'a polynome = Somme of 'a polynome * 'a polynome
  4. | Produit of 'a polynome * 'a polynome
  5. | Difference of 'a polynome * 'a polynome
  6. | Puissance of 'a polynome * int
  7. | Const of 'a
  8. | X;;
  9.  
  10. let p = Produit ( Somme (X, Const 2), Difference(X, Const 3));;
  11.  
  12. let monome const n = Produit( Const const, Puissance ( X, n));;
  13.  
  14. let q = monome 3. 2;;
  15.  
  16. let rec puissance k = fun 0 -> 1 | n -> k * puissance k (n-1);;
  17.  
  18. let rec evalue p valeur = match p with
  19. | Somme (q,r) -> evalue q valeur + evalue r valeur
  20. | Difference (q,r) -> evalue q valeur - evalue r valeur
  21. | Produit (q,r) -> evalue q valeur * evalue r valeur
  22. | Puissance (q, n) -> puissance (evalue q valeur) n
  23. | Const a -> a
  24. | X -> valeur;;
  25.  
  26. evalue p 3;;
  27.  
  28.  
  29. let rec keme liste = fun
  30. | 0 -> let t::q = liste in t
  31. | k -> let t::q = liste in keme q (k-1);;
  32.  
  33. let rec keme liste = fun
  34. | 0 -> hd liste
  35. | k -> keme (tl liste) (k-1);;
  36.  
  37. let renverse =
  38. let rec aux accu liste = match liste with
  39. | [] -> accu
  40. | t::q -> aux (t::accu) q
  41. in aux [];;
  42.  
  43. renverse [2; 3; 4];;
  44. ] -> failwith "Liste trop courte"
  45. |[x] -> x
  46. |t::q -> f t (reduce f q);;
  47.  
  48. reduce (prefix +) [2; 3; 4];;
  49. reduce max [2; 3; 4];;
  50.  
  51. let reduce2 f l =
  52. let rec aux accu l = match l with
  53. |[] -> accu
  54. |t::q -> aux (f accu t) q
  55. in aux (hd l) (tl l);;
  56.  
  57. it_list ;;
  58. list_it;;
  59.  
  60. (* Exercice 3 *)
  61. let longueur_term =
  62. let rec aux accu l = match l with
  63. |[]->accu
  64. |_::q -> aux (accu+1) q
  65. in aux 0;;
  66.  
  67. let rec l = 0::1::l;;
  68. (* Exercice 6 *)
  69.  
  70.  
  71. let rec divise l l1 l2 = match l, l1, l2 with
  72. |[], l1, l2 -> l1, l2
  73. |a::b, l1, l2 when list_length l mod 2=0 -> divise b (a::l1) l2
  74. |a::b, l1, l2 when list_length l mod 2=1 -> divise b l1 (a::l2)
  75. ;;
  76.  
  77. divise [4; 8; 90; 37; 79] [] [];;
  78.  
  79.  
  80. let rec fusion l l1 l2 = match l, l1, l2 with
  81. |l, [], [] -> rev l
  82. |l, a::b, c::d when a<=c -> fusion (a::l) b (c::d)
  83. |l, a::b, c::d when a>c -> fusion (c::l) (a::b) d
  84. |l, [], c::d -> fusion (c::l) [] d
  85. |l, a::b, [] -> fusion (a::l) b [];;
  86.  
  87.  
  88. fusion [] [4; 37; 73] [4;37; 63];;
  89.  
  90. let rec tri_fusion l = match (divise l [] []) with
  91. |l1, l2-> (fusion [] (tri_fusion l1) (tri_fusion l2 ))
  92. |[], []-> l;;
  93.  
  94. tri_fusion [2;1;35;7];;
  95.  
  96. let rec pol l = match l with
  97. |[] ->0
  98. |[a] -> int_of_string a
  99. |[a;b;c] -> match a with |+ -> int_of_string(b)+int_of_string(c)
  100. |- -> int_of_string(b)-int_of_string(c)
  101. |* -> int_of_string(b)*int_of_string(c)
  102. |/ -> int_of_string(b)/int_of_string(c)
  103. |a::b::c -> pol b;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement