Ocaml solution of problem 1