Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 3.89 KB | None | 0 0
  1. (* lists *)
  2.  
  3. let len(l) = List.length l;;
  4.  
  5. let fst(l) =
  6.   match l with
  7.     [] -> failwith "error fst : list is empty"
  8.   | hd::_ -> hd
  9. ;;
  10.  
  11. let rec lst(l) =
  12.   match l with
  13.     [] -> failwith "error lst : list is empty"
  14.   | hd::[] -> hd
  15.   | _::tail -> lst(tail)
  16. ;;
  17.  
  18. let nth(l, k) =
  19.   let rec nth1(l,k) =
  20.     match l with
  21.       []->  failwith "error nth : index must be in [0..len(l)-1]"
  22.     | hd::tail -> if k = 0 then hd else nth1(tail,k-1)
  23.   in
  24.   if k < 0
  25.   then failwith "error  nth : index must be positive"
  26.   else nth1(l,k)
  27. ;;
  28.  
  29.  
  30. let add_fst(l,e) = e::l ;;
  31.  
  32. let rec add_lst(l,e) =
  33.   match l with
  34.     [] -> [e]
  35.   | hd::tail -> hd::add_lst(tail,e);;
  36.  
  37. let add_nth(l, e, k) =
  38.   let rec add_nth1(l, e, k) =
  39.     match l with
  40.       [] -> [e]
  41.     | hd ::tail -> if k = 0 then e::l else hd::add_nth1(tail, e, k-1)
  42.   in
  43.   if k < 0
  44.   then failwith "error add_nth : index k must be positive"
  45.   else
  46.   if k > len(l)
  47.   then failwith "error add_nth : index must be in [0..len(l)]"
  48.   else add_nth1(l,e,k)
  49. ;;
  50.  
  51. let rem_fst(l) =
  52.   match l with
  53.     [] -> failwith "error rem_fst : list is empty"
  54.   | _::tail -> tail
  55. ;;
  56.  
  57. let rec rem_lst(l) =
  58.   match l with
  59.     [] -> failwith "error rem_lst : list is empty"
  60.   | [x] -> []
  61.   | x::tail -> x::rem_lst(tail)
  62. ;;
  63.  
  64. let rem_nth(l,k) =
  65.   let rec rem_nth1(l,k) =
  66.     match l with
  67.     | [] -> failwith "error rem_nth : index must be in [0..len(l)-1]"
  68.     | hd:: tail -> if k = 0 then tail else hd::rem_nth1(tail, k-1)
  69.   in
  70.   if k < 0
  71.   then failwith "error rem_nth :index k must be positive"
  72.   else rem_nth1(l,k)
  73. ;;
  74.  
  75. let concat(l1, l2) = l1 @ l2 ;;
  76.  
  77.  
  78. (* arrays *)
  79.  
  80. let arr_len(t) = Array.length t ;;
  81.  
  82. let arr_make(n, v) = Array.make n v ;;
  83.  
  84. (* aleatoire *)
  85.  
  86. let rand_self_init() = Random.self_init() ;;
  87.  
  88. let rand_init(n) = Random.init(n) ;;
  89.  
  90. let rand_int0(n) = Random.int(n+1) ;;
  91.  
  92. let rand_int(n, p) = Random.int(p-n + 1) + n ;;
  93.  
  94.  
  95.  
  96. (* lecture controlee d un caractere *)
  97.  
  98. let read_char() : char =
  99.   let s : string ref = ref "" and fin : bool ref = ref false in
  100.   (
  101.     while not(!fin)
  102.     do
  103.       s:= read_line() ;
  104.       if String.length(!s) = 0
  105.       then (print_string("erreur de saisie : vous n'avez saisi aucun caractère") ; print_newline() ;)
  106.       else fin := true;
  107.     done ;
  108.     (!s).[0] ;
  109.   )
  110. ;;
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. let list1 : char list = ['W';'Q';'a';'+'; 'F'];;
  167. let list2 : char list = ['2';'h';'a';'+'; 'r'];;
  168. let list3 : char list = ['t';'v';'a';'e'; 'f'];;
  169. let list4 : char list = ['@';'~';'=';'+'; '}'];;
  170.  
  171.  
  172. let rec tiny_letter_contain(list : char list) : bool =
  173.   if list <> []
  174.   then
  175.     if fst(list) >='a' && fst(list) <= 'z'
  176.     then true
  177.     else
  178.       tiny_letter_contain(rem_fst(list))
  179.   else
  180.     false
  181. ;;
  182.  
  183. tiny_letter_contain(list1);;
  184. tiny_letter_contain(list2);;
  185. tiny_letter_contain(list3);;
  186. tiny_letter_contain(list4);;
  187.  
  188.  
  189. let rec tiny_letter_count(list : char list) : int =
  190.   if tiny_letter_contain(list) = true
  191.   then if fst(list) >= 'a' && fst(list) <= 'z'
  192.     then tiny_letter_count(rem_fst(list)) + 1
  193.     else tiny_letter_count(rem_fst(list))
  194.   else
  195.     0
  196. ;;
  197.  
  198. (*#trace tiny_letter_count;;*)
  199.  
  200. tiny_letter_count(list1);;
  201. tiny_letter_count(list2);;
  202. tiny_letter_count(list3);;
  203. tiny_letter_count(list4);;
  204.  
  205.  
  206. let rec tiny_list(list, iter : char list * int) : char list =
  207.   if len(list) <> tiny_letter_count(list)
  208.   then if not(nth(list, iter) >= 'a' && nth(list, iter) <= 'z')
  209.     then tiny_list(rem_nth(list, iter), iter)
  210.     else
  211.       tiny_list(list, iter + 1)
  212.   else
  213.     list
  214. ;;
  215.  
  216.  
  217. let tiny_letter_sub_list(list : char list) : char list =
  218.   if tiny_letter_contain(list) = true
  219.   then tiny_list(list, 0)
  220.   else
  221.     []
  222. ;;
  223.  
  224.  
  225.  
  226. #trace tiny_letter_sub_list;;
  227.  
  228. tiny_letter_sub_list(list1);;
  229. tiny_letter_sub_list(list2);;
  230. tiny_letter_sub_list(list3);;
  231. tiny_letter_sub_list(list4);;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement