Number Parsing in Prolog
Our first example demonstrates how to parse numbers from strings in Prolog. This is a common task in many programs.
To run the program, save it as number_parsing.pl
and use the Prolog interpreter:
In Prolog, number parsing is handled differently compared to some other languages:
atom_number/2
is used to parse both floating-point numbers and integers.number_codes/2
can be used to parse integers, including hexadecimal numbers.- There’s no separate function for parsing unsigned integers; Prolog uses arbitrary-precision integers.
- Instead of returning an error, parsing predicates in Prolog typically fail when given invalid input.
Next, we’ll look at another common parsing task: URLs.