Parsing numbers from strings is a basic but common task in many programs; here’s how to do it in OCaml.
To run the program, save it as number_parsing.ml and use ocamlc to compile and run:
In OCaml, number parsing is handled by built-in modules like Float and Int. Unlike Go, OCaml doesn’t have a separate unsigned integer type in its standard library, so we use Int64 for larger numbers.
OCaml’s parsing functions raise exceptions on invalid input, which we can catch with a try-with block. This is different from Go’s approach of returning an error value.
The Printf module is used for formatted output, similar to Go’s fmt package.
Next, we’ll look at another common parsing task: URLs.