Javatpoint Logo
Javatpoint Logo

Input and Output Terms in Prolog

Prolog provides the facility to enable the input and output either of character or of terms. It is simpler to use terms. Initially, we will assume that all input is from the keyboard of user and all output is at the screen of the user. In this section, we will describe input and output using external files like CR-ROM or hard disk. Just like many other built-in predicates, the predicates of input and output are unsatisfiable that means while backtracking they always fail.

Output Terms

To provide the input terms, we will use a built-in predicate write/1. This predicate takes a single argument. When we evaluate the predicate, the term will be written to the current output stream. By default, the current output stream is the screen of user.

In this tutorial, we have been used many times a built-in predicate n1/0. This predicates takes no arguments. When we evaluate nl predicate, this will cause a new line to be output to the current output stream.

For Example:

If the atoms have quoted on input, because of write predicate they are not quoted on output. If we want to output the quotes, we can use the writeq/1 predicate.

For example:

Input Terms

To provide the input terms, we will use a built-in predicate read/1. This predicate takes a single argument and that argument must be a variable. When we evaluate this predicate, due to this the next term will be read from the current input stream. By default, the current input stream is the keyboard of the user.

In the input stream, at least one white space character or new line and a dot('.') follows the term. The white space and dot are not part of the term, but they are read in.

Note that if the user input is required from the keyboard, a prompt character like colon will be displayed. Before Prolog accepts the input from user, we have to press the 'return' key.

When Prolog evaluates a read goal, the input term is unified with the variable of the argument. If the variable of the argument is unbound, it is bound to the input value.

For example:

If the variable of argument is already bound, the goal succeeds if and only if the previously bound value and input term are identical.

For example:

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA