Skip to content

Instantly share code, notes, and snippets.

@davidsan
Created January 5, 2013 20:17
Show Gist options
  • Select an option

  • Save davidsan/4463419 to your computer and use it in GitHub Desktop.

Select an option

Save davidsan/4463419 to your computer and use it in GitHub Desktop.
exception Invalid_Phone_Key
type noeud_lex = Lettre of char * bool * arbre_lex
and arbre_lex = noeud_lex list ;;
(* q1 *)
let left_dict = [Lettre ('A', true, [Lettre ('R', true, []);
Lettre ('U', false, [Lettre ('X', true, [])])])]
let right_dict = [Lettre ('R', false, [Lettre ('I', false, [Lettre ('S', false, [Lettre ('E', false, [])]);
Lettre ('T', true, [Lettre ('E', true, [])])])])]
let f_dict = [Lettre ('F', true, left_dict@right_dict)]
(* q2 *)
let get_chars x =
match x with
| 1 -> [' ']
| 2 -> ['A'; 'B'; 'C']
| 3 -> ['D'; 'E'; 'F']
| 4 -> ['G'; 'H'; 'I']
| 5 -> ['J'; 'K'; 'L']
| 6 -> ['M'; 'N'; 'O']
| 7 -> ['P'; 'Q'; 'R'; 'S']
| 8 -> ['T'; 'U'; 'V']
| 9 -> ['W'; 'X'; 'Y'; 'Z']
| _ -> raise Invalid_Phone_Key
(* q3 *)
let q3 x t =
let letters = get_chars x in
List.filter (fun e -> match e with
| Lettre(c, true, _) -> List.mem c letters
| Lettre(_, false,_) -> false) t
;;
(* test *)
(*
let test3 = q3 2 left_dict;;
let rec print_noeud_lex node = match node with Lettre(c, _, l) -> Printf.printf "%c " c; List.iter print_noeud_lex l;;
let print_arbre_lex tree = List.iter print_noeud_lex tree;;
print_arbre_lex test3;; *)
(* q4 *)
let q4 x t =
let roots = q3 x t in
List.fold_left (fun acc ele -> match ele with Lettre(_, _, l) -> acc@l) [] roots
(* let test4 = q4 2 left_dict;;
print_arbre_lex test4;;
*)
(* q5 *)
let q5 x t =
let roots = q3 x t in
(roots, List.fold_left (fun acc ele -> match ele with Lettre(_, _, l) -> acc@l) [] roots)
(* q6 *)
let rec q6 xs dict =
match xs with
| [] -> []
| hd::tl ->
let cpl = q5 hd dict
in (fst cpl)@(q6 tl (snd cpl))
(* q7 : parcours d'arbre avec vérification du booléen *)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment