Problem 31: OCaml solution.