Looping until a Condition is Satisfied

To execute a set of instructions repeatedly until a given condition is met, many programming languages provide an 'until loop'. Again, Prolog has no looping facility, but we can obtain a similar effect in several ways.

Recursion

The following program shows the use of recursion. It reads the terms which are entered by the user from the keyboard, and it produces term as the output on the screen until the end is encountered.


In the above example, the program can be written in a single clause using the ;/2 disjunction operator.

If variable Word is bound to the atom end, the disjunction goal (Word=end;loop) succeeds. If it is not, the system recursively attempts to satisfy the goal loop.

The following recursive program repeatedly prompts the user to enter a term until the user enters yes or not.


Use of 'repeat' predicate

In the Prolog program, the easiest way to provide the type of looping is not always recursion. Another method to provide the looping is built-in predicate repeat.

The goal repeat does not mean that it repeats anything. Whenever it called, it merely succeeds. The value of repeat also succeeds while backtracking. Due to this effect, the order of evaluating goals is changed by any other succeeding goal from 'right to left' back to 'left to right'. This predicate can also create a looping effect.

The following program prompts the user repeatedly to enter a term until the user enters either yes or no. This program is an alternative to the previous program.

In the body of get_answer, the first five goals will always succeed. When we evaluate the 5th goal read(Answer), it will prompt the user to specify the term. The goal valid((Answer) will fail and say unsure if the input of term is not yes or no. Now Prolog will backtrack over read(Answer) and write('yes or no answer'). Both the goals are unsatisfiable that means while backtracking, it will always fail.

Now backtracking will go to the repeat predicate and succeed. Due to this, the evaluation will again proceed forward(left-to-right) and write('yes or no answer') and read(Answer) both succeeding. Now Prolog will further evaluate the goal valid(Answer).

On the basis of value of Answer, which is input by the user, the goal valid(Answer) will succeed or fail. If this goal succeeds, the final three goals will succeed that is write('The correct answer is '), write(Answer), and nl. If this goal fails, Prolog will backtrack to the predicate repeat. The overall effect shows that write('yes or no answer') and read(Answer) both goals are repeatedly called until goal valid(Answer) is satisfied, which is the terminating condition.

In the body of a clause, the left of repeat goals will never be reached on backtracking.

The following program reads a sequence of terms from the specified file. It outputs those terms to the current output stream until we encountered the end of term.

The following program defines a loop between the goals repeat and Y=end, just like the previous program.

The following line contains in the myfile.txt file.

If we call readterms, the following output will be produced: