OCaml translation of the code from "Algorithms: A Functional Programming Approach"