% bottles2.pl

% Solution using backtracking
%
bottles(X) :-
	retractall(stock(_)),
	asserta(stock(X)),
	bottles.
bottles :-
	repeat,
 	retract(stock(Old)),
	(
	  Old = 0,
	    format("Now, they are all gone."), !, fail;
	  Old = 1,  
	    format("1 bottle of beer on the wall. ");
	  Old > 1,
	    format("~d bottles of beer on the wall. ", Old)
	),
	take_one(Old,New),
	assertz(stock(New)),
	fail.

take_one(In,Out) :-
	Out is In - 1,
	format("Take one down, and pass it around.\n").
