Values in Prolog
Our first example will demonstrate various value types in Prolog including atoms, numbers, and boolean expressions. Here’s the full source code:
To run the program, save the code in a file (e.g., values.pl
) and use a Prolog interpreter:
Let’s break down the example:
Atoms: In Prolog, atoms are similar to strings in other languages. We use the
atom_concat/3
predicate to concatenate two atoms.Numbers: Prolog supports both integers and floating-point numbers. Arithmetic operations are performed using the
is
operator.Boolean expressions: Prolog uses
,
for logical AND,;
for logical OR, and\+
for logical NOT. The->
operator is used for if-then-else constructs.Output: We use
writeln/1
for simple output andformat/2
for formatted output.
Note that Prolog is a logic programming language, so the syntax and concepts differ significantly from imperative languages. The main/0
predicate serves as the entry point of our program.