(*** Keep these lines. ***) let qfile = "qqs.ml" ;; let formatter = Format.(make_formatter (fun _ _ _ -> ()) (fun () -> ())) ;; Topdirs.dir_use formatter (if Sys.file_exists qfile then qfile else Sys.getenv "HOME" ^ "/Exam-OCaml/" ^ qfile) ;; (************************************) (***** QUESTION 1 *****) let f1 f x = try f x with | Invalid_argument s -> raise (Failure s) | Failure s -> raise (Invalid_argument s) ;; (* ✔✔✔ Check your answer - keep the following line untouched. If you get a type error here, it means that your function f1 does not have the expected type. *) let () = q1 { ff = f1 } ;; (***** QUESTION 2 *****) let f2 a b l = (* 2 - Go until first b *) let rec loop2 acu = function | [] -> List.rev acu | x :: xs -> if x = b then List.rev (b :: acu) else loop2 (x :: acu) xs in (* 1 - Find first a *) let rec loop1 = function | [] -> [] | x :: xs -> if x = a then loop2 [a] xs else loop1 xs in loop1 l ;; (* ✔✔✔ Check your answer - keep the following line untouched. If you get a type error here, it means that your function f2 does not have the expected type. *) let () = q2 { ff = f2 } ;; (***** QUESTION 3 *****) let f3 tree = let rec loop acu (Node (x,l)) = List.fold_left loop (x :: acu) l in loop [] tree ;; (* ✔✔✔ Check your answer - keep the following line untouched. If you get a type error here, it means that your function f3 does not have the expected type. *) let () = q3 { ff = f3 } ;; (***** QUESTION 4 *****) let f4 = { forbid = (function Some 0 -> "zero" | _ -> "") ; f = (function None -> false | Some x -> x > 0 ) } ;; (* ✔✔✔ Check your answer - keep the following line untouched. If you get a type error here, it means that your function f4 does not have the expected type. *) let () = q4 { ff = f4 } ;; (***** QUESTION 5 *****) let f5 f = { forbid = (fun (a,b) -> f.forbid (b,a)) ; f = (fun (a,b) -> f.f (b,a)) } ;; (* ✔✔✔ Check your answer - keep the following line untouched. If you get a type error here, it means that your function f5 does not have the expected type. *) let () = q5 { ff = f5 } ;; (***** QUESTION 6 *****) let f6 f = let forbid l = match List.find_opt (fun x -> f.forbid x <> "") l with | None -> "" | Some x -> f.forbid x and f l = List.fold_left (fun r x -> r && f.f x) true l in { forbid ; f } ;; (* ✔✔✔ Check your answer - keep the following line untouched. If you get a type error here, it means that your function f6 does not have the expected type. *) let () = q6 { ff = f6 } ;; (***** QUESTION 7 *****) let f7 x graph = let rec loop acu x = let succ = List.filter_map (fun (a,b) -> if a = x then Some b else None) graph in if List.mem x acu then acu else List.fold_left (fun acu y -> loop acu y) (x :: acu) succ in loop [] x ;; (* ✔✔✔ Check your answer - keep the following line untouched. If you get a type error here, it means that your function f7 does not have the expected type. *) let () = q7 { ff = f7 } ;;