% 1. Internal database to count % Only to contain facts but not rules. % 2. Use of a loop by 'repeat' % 3. Determ predicates not to be resatisfied on backtracking, a useful % feature especially when a predicate is to be used in the middle % of a loop rather than at the top of a loop. % 4. Use of CUT to control backtracking. domains number=integer predicates repeat fact(number,number) run(number) determ output(number) %Deterministic anyway as its %subgoals are all built-in. database count(integer) clauses repeat. repeat :- repeat. count(0). fact(0,1) :- !. %% This CUT is critical. fact(N,Answer) :- New=N-1, fact(New,Temp), Answer=N*Temp. run(N) :- repeat, %% Redundant as 'count' is resatisfied %% on backtracking. retract(count(Previous)), Next=Previous+1, asserta(count(Next)), fact(Next,Ans), output(Ans), Next = N. run(_). output(Ans):- write(Ans), nl.