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) ;;
let f1 g l = List.fold_left (fun acu x -> acu + g x) 0 l
let () = q1 { ff = f1 }
let f2 g x = ( g x (fun f y -> f y x) )
let () = q2 { ff = f2 }
let f3 g y = function
| None -> y
| Some z -> g z
let () = q3 { ff = f3 }
let rec f4 g = function
| [] -> []
| A x :: rest -> let tail = f4 g rest in (try g x :: tail with _ -> tail)
| B x :: rest -> g x :: f4 g rest
let () = q4 { ff = f4 }
let f5 f l = List.for_all (function A _ -> true | B y -> f y) l
let () = q5 { ff = f5 }
let f6 l =
let alla = List.filter_map (function A x -> Some x | _ -> None) l in
let rec loop alla l =
match (alla, l) with
| [], [] -> []
| xa :: xrest, A y :: rest -> A xa :: loop xrest rest
| _, B y :: rest -> B y :: loop alla rest
| [], A _ :: _ -> assert false
| _ :: _, [] -> assert false
in
loop (List.rev alla) l
let () = q6 { ff = f6 }
let rec f7 f = function
| Aleaf x -> Aleaf x
| Bleaf y -> if f y then Aleaf y else Bleaf y
| Node (left, right) -> Node (f7 f left, f7 f right)
let () = q7 { ff = f7 }
let rec f8 t1 t2 =
match t1, t2 with
| Aleaf x, Aleaf y -> if x <> y then Some (t1, t2) else None
| Bleaf x, Bleaf y -> if x <> y then Some (t1, t2) else None
| Aleaf _, _
| Bleaf _, _
| _, Aleaf _
| _, Bleaf _ -> Some (t1, t2)
| Node (l1, r1), Node (l2, r2) ->
begin match f8 l1 l2 with
| None -> f8 r1 r2
| Some z -> Some z
end
let () = q8 { ff = f8 }