type html_tree = | Empty | Html of html_tree list | P of html_tree list | B of html_tree list | I of html_tree list | Word of string let ssarbre1 = Word "dans" let ssarbre2 = B([Word "un" ]) let ssarbre3= Word "Mot" let larbre1 = [ ssarbre1 ; ssarbre2; ssarbre3 ] let larbre2 = [ ssarbre1 ; ssarbre3; ssarbre2 ] let arbre1 = Html ([ P larbre1 ; Word "x" ]) let arbre2 = Html ([ P larbre2 ; Word "x" ]) let nombre_ssarbre_immediats t = match t with | Empty -> 0 | Word _ -> 0 | Html l | B l | P l | I l -> List.length l;; nombre_ssarbre_immediats arbre1;; nombre_ssarbre_immediats ssarbre1;; let rec affiche_si_word l = match l with | [] -> "" | Word s :: l' -> s ^ " "^ affiche_si_word l' | _ :: l' -> affiche_si_word l';; let affiche_si_word l = List.iter (fun a -> match a with | Word s -> print_string (s ^ " ") | _ -> ()) l ;; affiche_si_word larbre;; let rec nombre_noeuds t = match t with | Empty -> 0 | Word _ -> 1 | Html l | P l | B l | I l -> 1 + nombre_noeuds_list l and nombre_noeuds_list l = match l with | [] -> 0 | a :: l' -> nombre_noeuds a + nombre_noeuds_list l';; nombre_noeuds arbre1;; let rec nombre_words t = match t with | Empty -> 0 | Word _ -> 1 | Html l | P l | B l | I l -> nombre_words_list l and nombre_words_list l = match l with | [] -> 0 | a :: l' -> let na = nombre_words a in let nl' = nombre_words_list l' in na + nl' ;; let rec nombre_words_prec (b:bool) (t:html_tree) : int*bool = match t with | Empty -> 0,false | Word _ -> (if b then (1,true) else (0,true)) | Html l | P l | B l | I l -> (nombre_words_list_prec false l) , false and nombre_words_list_prec (b:bool) l : int = match l with | [] -> 0 | a :: l' -> let na,b' = nombre_words_prec b a in let nl' = nombre_words_list_prec b' l' in na + nl' ;;