1.ml 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. let rec longueur l = match l with
  2. | [] -> 0
  3. | t::q -> 1 + longueur q;;
  4. let rec appartient x l = match l with
  5. | [] -> false
  6. | t::q -> if t = x then
  7. true
  8. else
  9. appartient x q;;
  10. let rec occurence x l = match l with
  11. | [] -> 0
  12. | t::q -> if t = x then
  13. 1+ occurence x q
  14. else
  15. occurence x q;;
  16. let rec dernier l = match l with
  17. | [a] -> a
  18. | t::q -> dernier q;;
  19. let rec ajout_fin x l = match l with
  20. | [] -> [x]
  21. | t::q -> t::ajout_fin x q;;
  22. let rec ieme i l = let x::xs=l in
  23. if i = 0 then x else ieme (i-1) xs
  24. let rec supprime i l = let t::q =l in
  25. match i with
  26. | 1 -> List.tl l
  27. | _ -> t::(supprime (i-1) q);;
  28. let rec supprimetout x l = match l with
  29. | [] -> []
  30. | t::q ->
  31. if t=x then
  32. supprimetout x q
  33. else
  34. t::(supprimetout x q);;
  35. let rec insere x i l= let t::q=l in
  36. match i with
  37. | 1 -> x::l
  38. | _ -> t::(insere x (i-1) q);;
  39. let rec concatene l1 l2 =
  40. match l2 with
  41. | [] -> l1
  42. | t::q -> t::(concatene l1 q);;
  43. let rec concatene_inverse l1 l2 =
  44. match l1 with
  45. | [] -> List.rev l2
  46. | x :: xs -> concatene_inverse xs (x :: l2);;
  47. let rec mirroir l =
  48. match l with
  49. | [] -> []
  50. | x :: xs -> mirroir xs @ [x];;
  51. let rec addition l1 l2 =
  52. match (l1, l2) with
  53. | ([], _) -> l2
  54. | (_, []) -> l1
  55. | (x :: xs, y :: ys) -> (x + y) :: addition xs ys
  56. ;;
  57. let rec fusion l1 l2 =
  58. match (l1, l2) with
  59. | ([], _) -> l2
  60. | (_, []) -> l1
  61. | (x :: xs, y :: ys) ->
  62. if x <= y then
  63. x :: fusion xs l2
  64. else
  65. y :: fusion l1 ys;;
  66. let rec somme l = match l with
  67. | [] -> 0
  68. | t::q -> t+somme q;;
  69. let rec maximum l =
  70. match l with
  71. | [] -> failwith "empty list"
  72. | [x] -> x
  73. | x :: xs ->
  74. let max_tail = maximum xs in
  75. if x > max_tail then x else max_tail
  76. let rec second_max l =
  77. match l with
  78. | [] | [_] -> failwith "not enough elements"
  79. | [x; y] -> if x > y then y else x
  80. | x :: xs ->
  81. let (max_tail, second_max_tail) = (maximum xs, second_max xs) in
  82. if x > max_tail then
  83. if max_tail > second_max_tail then max_tail else second_max_tail
  84. else
  85. if x > second_max_tail then x else second_max_tail
  86. let rec evaluation p x =
  87. match p with
  88. | [] -> 0.0
  89. | a :: [] -> a
  90. | a :: b :: xs ->
  91. let q = b +. x *. evaluation xs x in
  92. evaluation (q :: xs) x +. a
  93. let rec binomial n p = match (n,p) with
  94. | (n,0) -> 1
  95. | (n,p) when p=n -> 1
  96. | _ -> binomial(n-1) p + binomial (n-1) (p-1);;
  97. let rec ligne_suivante l =
  98. match l with
  99. | [] -> [1]
  100. | x :: xs ->
  101. let rec aux acc prev =
  102. match prev with
  103. | [] -> List.rev (1 :: acc)
  104. | y :: ys -> aux ((x + y) :: acc) ys
  105. in aux [] l;;
  106. let rec pascal n =
  107. match n with
  108. | 0 -> []
  109. | 1 -> [[1]]
  110. | _ ->
  111. let prev = pascal (n - 1) in
  112. let last = List.hd (List.rev prev) in
  113. let next = ligne_suivante last in
  114. prev @ [next]