99 Bottles of Beer/Prolog: Difference between revisions

Content added Content deleted
(moving code from main task-page to sub-page / Visual Prolog)
Line 12: Line 12:
=={{header|Prolog}}==
=={{header|Prolog}}==
{{works with|SWI Prolog}}
{{works with|SWI Prolog}}
<lang prolog>bottles(0):-!.
<lang prolog>bottles(0).
bottles(X):-
bottles(X):-
writef('%t bottles of beer on the wall \n',[X]),
writef('%t bottles of beer on the wall \n',[X]),
Line 23: Line 23:
:- bottles(99).</lang>
:- bottles(99).</lang>


It is possible to extend the previous version so to handle singular and plurals of the word 'bottle':
An other version that handles plural/not plural conditions.

<lang prolog>bottles(0):- write('no more bottles of beer').
bottles(B) :-
dif(B,0), NewB is B - 1,
(
dif(B,1), writef('%t bottles of beer on the wall \n',[B]), writef('%t bottles of beer\n',[B]);
writef('%t bottle of beer on the wall \n',[B]), writef('%t bottle of beer\n',[B])
),
write('Take one down, pass it around\n'),
(
NewB > 1, writef('%t bottles of beer on the wall \n\n',[NewB]);
NewB =:= 1, writef('%t bottle of beer on the wall \n\n',[NewB]);
true
),
bottles(NewB).

:- bottles(99).</lang>

Another version that handles plural/not plural conditions.


<lang prolog>line1(X):- line2(X),write(' on the wall').
<lang prolog>line1(X):- line2(X),write(' on the wall').