Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* lists *)
- let len(l) = List.length l;;
- let fst(l) =
- match l with
- [] -> failwith "error fst : list is empty"
- | hd::_ -> hd
- ;;
- let rec lst(l) =
- match l with
- [] -> failwith "error lst : list is empty"
- | hd::[] -> hd
- | _::tail -> lst(tail)
- ;;
- let nth(l, k) =
- let rec nth1(l,k) =
- match l with
- []-> failwith "error nth : index must be in [0..len(l)-1]"
- | hd::tail -> if k = 0 then hd else nth1(tail,k-1)
- in
- if k < 0
- then failwith "error nth : index must be positive"
- else nth1(l,k)
- ;;
- let add_fst(l,e) = e::l ;;
- let rec add_lst(l,e) =
- match l with
- [] -> [e]
- | hd::tail -> hd::add_lst(tail,e);;
- let add_nth(l, e, k) =
- let rec add_nth1(l, e, k) =
- match l with
- [] -> [e]
- | hd ::tail -> if k = 0 then e::l else hd::add_nth1(tail, e, k-1)
- in
- if k < 0
- then failwith "error add_nth : index k must be positive"
- else
- if k > len(l)
- then failwith "error add_nth : index must be in [0..len(l)]"
- else add_nth1(l,e,k)
- ;;
- let rem_fst(l) =
- match l with
- [] -> failwith "error rem_fst : list is empty"
- | _::tail -> tail
- ;;
- let rec rem_lst(l) =
- match l with
- [] -> failwith "error rem_lst : list is empty"
- | [x] -> []
- | x::tail -> x::rem_lst(tail)
- ;;
- let rem_nth(l,k) =
- let rec rem_nth1(l,k) =
- match l with
- | [] -> failwith "error rem_nth : index must be in [0..len(l)-1]"
- | hd:: tail -> if k = 0 then tail else hd::rem_nth1(tail, k-1)
- in
- if k < 0
- then failwith "error rem_nth :index k must be positive"
- else rem_nth1(l,k)
- ;;
- let concat(l1, l2) = l1 @ l2 ;;
- (* arrays *)
- let arr_len(t) = Array.length t ;;
- let arr_make(n, v) = Array.make n v ;;
- (* aleatoire *)
- let rand_self_init() = Random.self_init() ;;
- let rand_init(n) = Random.init(n) ;;
- let rand_int0(n) = Random.int(n+1) ;;
- let rand_int(n, p) = Random.int(p-n + 1) + n ;;
- (* lecture controlee d un caractere *)
- let read_char() : char =
- let s : string ref = ref "" and fin : bool ref = ref false in
- (
- while not(!fin)
- do
- s:= read_line() ;
- if String.length(!s) = 0
- then (print_string("erreur de saisie : vous n'avez saisi aucun caractère") ; print_newline() ;)
- else fin := true;
- done ;
- (!s).[0] ;
- )
- ;;
- let list1 : char list = ['W';'Q';'a';'+'; 'F'];;
- let list2 : char list = ['2';'h';'a';'+'; 'r'];;
- let list3 : char list = ['t';'v';'a';'e'; 'f'];;
- let list4 : char list = ['@';'~';'=';'+'; '}'];;
- let rec tiny_letter_contain(list : char list) : bool =
- if list <> []
- then
- if fst(list) >='a' && fst(list) <= 'z'
- then true
- else
- tiny_letter_contain(rem_fst(list))
- else
- false
- ;;
- tiny_letter_contain(list1);;
- tiny_letter_contain(list2);;
- tiny_letter_contain(list3);;
- tiny_letter_contain(list4);;
- let rec tiny_letter_count(list : char list) : int =
- if tiny_letter_contain(list) = true
- then if fst(list) >= 'a' && fst(list) <= 'z'
- then tiny_letter_count(rem_fst(list)) + 1
- else tiny_letter_count(rem_fst(list))
- else
- 0
- ;;
- (*#trace tiny_letter_count;;*)
- tiny_letter_count(list1);;
- tiny_letter_count(list2);;
- tiny_letter_count(list3);;
- tiny_letter_count(list4);;
- let rec tiny_list(list, iter : char list * int) : char list =
- if len(list) <> tiny_letter_count(list)
- then if not(nth(list, iter) >= 'a' && nth(list, iter) <= 'z')
- then tiny_list(rem_nth(list, iter), iter)
- else
- tiny_list(list, iter + 1)
- else
- list
- ;;
- let tiny_letter_sub_list(list : char list) : char list =
- if tiny_letter_contain(list) = true
- then tiny_list(list, 0)
- else
- []
- ;;
- #trace tiny_letter_sub_list;;
- tiny_letter_sub_list(list1);;
- tiny_letter_sub_list(list2);;
- tiny_letter_sub_list(list3);;
- tiny_letter_sub_list(list4);;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement