?- use_module(library(random)). % this predicate loops as follows: It asks the user for a cmd. If it is % listing, a listing is done. If random, then a random number is % written. If it is end, then the loop quits. Anything else, and an % error message is written. % This version uses a failure-driven loop to do iteration. % Looping is started with repeat, and continues until quit is entered. % Cuts are used to prevent backtracking, which also reduces memory usage % when recursing, as there is no need to retain the tree for backtrack. cmdLooper :- repeat, write('enter command (listing, random, or quit): '), read(Term), do_cmd(Term), Term = quit. do_cmd(quit) :- write('Quitting.'), nl. do_cmd(listing) :- !, write('Listing...'), nl, listing. do_cmd(random) :- !, write('Random number: '), random(X), write(X), nl. do_cmd(X) :- write('No siree Bob! Bad command: '), write(X), write('.'), nl.