Functions in Prolog
Functions are central in Prolog. We’ll learn about functions (also known as predicates in Prolog) with a few different examples.
To run this Prolog program, you would typically save it in a file (e.g., functions.pl
) and then consult it in a Prolog interpreter:
In Prolog, we define predicates instead of functions. Predicates in Prolog are relations between their arguments, rather than functions that return values. However, we can use them to achieve similar results.
The is
operator in Prolog is used for arithmetic evaluation. It calculates the expression on its right side and unifies the result with the variable on its left side.
Prolog uses pattern matching and backtracking to solve goals, which is quite different from imperative languages. This example demonstrates basic arithmetic operations and output formatting in Prolog.
There are several other features to Prolog predicates. One is the ability to have multiple solutions, which we’ll look at in future examples.