------ 1. c) c) a) ------ 2. bool -> bool list -> bool list ------ 3. type 'a tree = Nil | Node of 'a * 'a tree * 'a tree ;; type 'a ntree = NNil | NNode of 'a * 'a ntree list ;; type doc = string ntree ;; let alice = NNode("Alice's Adventures in Wonderland, by Lewis Carroll", [ NNode("CHAPTER I - Down the Rabbit-Hole", [ NNode("Alice was beginning to get very tired...",[]); NNode("peeped into the book her sister was reading, but...",[]); NNode("Alice 'without pictures or conversations?'",[]) ]); NNode("CHAPTER II - The Pool of Tears", [ NNode("'Curiouser and curiouser!' cried Alice...",[]); NNode("English); 'now I'm opening out like the largest telescope that...",[]); NNode("seemed to be almost out of sight, they were getting so...",[]) ]) ]);; a) let rec validate t = match t with NNil -> true | NNode(x, cs) -> String.length x <= 80 && List.for_all validate cs ;; b) let rec getX t l = match t, l with NNil, _ -> NNil | _, [] -> t | NNode(i, cs), x::xs -> getX (List.nth cs (x-1)) xs ;; let get t l = try getX t l with _ -> NNil ;; c) let rec sum f l = match l with [] -> 0 | x::xs -> f x + sum f xs ;; let rec print_length t = match t with NNil -> 0 | NNode(x, []) -> 1 | NNode(x, cs) -> 3 + sum print_length cs ;; Outra maneira: let rec print_length t = match t with NNil -> 0 | NNode(x, []) -> 1 | NNode(x, cs) -> 3 + lprint_length cs and lprint_length l = match l with [] -> 0 | t::ts -> print_length t + lprint_length ts ;; d) let pageSize = 5;; let rec pag t n = match t with NNil -> (t, n) | NNode(x, []) -> if n <= pageSize - 1 then (NNode(x, []), n+1) else (NNode("\012" ^ x, []), 1) | NNode(x, cs) -> if n <= pageSize - 3 then let (l, b) = lpag cs (n + 3) in (NNode(x, l), b) else let (l, b) = lpag cs 3 in (NNode("\012" ^ x, l), b) and lpag l n = match l with [] -> ([], n) | t::ts -> let (a, b) = pag t n in let (c, d) = lpag ts b in (a::c, d) ;; let paginate t = fst (pag t 0);; validate alice ;; get alice [1;3];; get alice [1;3;3];; print_length alice;; paginate alice ;;