?- 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 of the program uses recursion to continually loop. % The only time recursion doesn't happen is if 'quit' is entered. % The cuts are used to prevent backtracking. cmdLooper :- write('enter command (listing, random, or quit): '), read(Term), do_cmd(Term). do_cmd(quit) :- !, write('Quitting.'), nl. do_cmd(listing) :- write('Listing...'), nl, listing, !, cmdLooper. do_cmd(random) :- write('Random number: '), random(X), write(X), nl, !, cmdLooper. do_cmd(X) :- write('No siree Bob! Bad command: '), write(X), write('.'), nl, !, cmdLooper.