(*** 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 tos x =
try tos (f x)
with _ -> "exception"
;;
(* ✔✔✔ 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 =
(* Ignore part until b *)
let rec loop2 acu = function
| [] -> List.rev acu
| x :: xs ->
if x = b then List.rev (List.rev_append xs acu)
else loop2 acu xs
in
(* Keep first part until a *)
let rec loop1 acu = function
| [] -> List.rev acu
| x :: xs -> if x = a then loop2 acu xs else loop1 (x :: acu) 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 (Node (x,_) as tree) =
let rec loop acu (Node (x, l)) = List.fold_left loop (max x acu) l in
loop x 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 = (fun (a,b) -> if a = b then "equal" else "") ;
f = (fun (a,b) -> a < b) }
;;
(* ✔✔✔ 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 = (function None -> "" | Some x -> f.forbid x) ;
f = (function None -> false | Some x -> f.f x) }
;;
(* ✔✔✔ 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 =
if l = [] then ""
else if List.for_all (fun x -> f.forbid x <> "") l then "all" else ""
and f l = List.exists (fun x -> f.forbid x = "" && f.f x) 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 } ;;