% printn.pl
% Here are three versions of the printn() predicate covered in class.
% (print values 1, 2, 3, ... , N)

% V1 using a separate clause for stopping condition
printn(0) :- !.	% a cut (!) gets "yes" right away and stops searching
printn(N) :- M is N-1, printn(M), write(N), nl.

% V2 with stopping condition in one clause
% This mimics if-else, which is not really the "Prolog way"
printnV2(N) :- (N =:= 0 -> !; M is N-1, printnV2(M), write(N), nl).
                       %   ^  ^
                       %   |  +--- this is the false part
                       %   +-- this is the true part

% V3 with tail recursion
% Tail recursion means the last thing is the recursive call (the tail)
printnV3(N) :- printn1(N,1).
printn1(N,Count) :- Count > N, !.   % cut off search space
printn1(N,Count) :- write(Count), nl, Count1 is Count+1, printn1(N,Count1).
