Our example demonstrates how Prolog handles references and modifications to data. In Prolog, we use logical variables and unification instead of pointers.
To run this Prolog program:
In this Prolog version, zero_val doesn’t change the I in main, but zero_ref does because it uses a mutable reference to modify the value. The concept of pointers is approximated using Prolog’s logical variables and compound terms with mutable arguments.
Note that Prolog’s approach to data and variables is fundamentally different from imperative languages. Variables in Prolog are logical entities that can be unified with values, rather than mutable storage locations. The use of nb_setarg for mutable state is generally discouraged in pure logical programming, but it’s used here to demonstrate a concept similar to pointer modification.