99 Bottles of Beer/Prolog: Difference between revisions

From Rosetta Code
Content added Content deleted
(new subpage)
 
(moving code from main task-page to sub-page)
Line 4: Line 4:
{{collection|99 Bottles of Beer}}
{{collection|99 Bottles of Beer}}
[[99 Bottles of Beer]] done in Prolog-languages
[[99 Bottles of Beer]] done in Prolog-languages
<!--
See [[99 Bottles of Beer/Prolog]]
-->

__toc__
__toc__

=={{header|Prolog}}==
{{works with|SWI Prolog}}
<lang prolog>bottles(0):-!.
bottles(X):-
writef('%t bottles of beer on the wall \n',[X]),
writef('%t bottles of beer\n',[X]),
write('Take one down, pass it around\n'),
succ(XN,X),
writef('%t bottles of beer on the wall \n\n',[XN]),
bottles(XN).

:- bottles(99).</lang>

An other version that handles plural/not plural conditions.

<lang prolog>line1(X):- line2(X),write(' on the wall').
line2(0):- write('no more bottles of beer').
line2(1):- write('1 bottle of beer').
line2(X):- writef('%t bottles of beer',[X]).
line3(1):- write('Take it down, pass it around').
line3(X):- write('Take one down, pass it around').
line4(X):- line1(X).
bottles(0):-!.
bottles(X):-
succ(XN,X),
line1(X),nl,
line2(X),nl,
line3(X),nl,
line4(XN),nl,nl,
!,
bottles(XN).
:- bottles(99).</lang>

Revision as of 11:59, 21 November 2014

99 Bottles of Beer/Prolog is part of 99 Bottles of Beer. You may find other members of 99 Bottles of Beer at Category:99 Bottles of Beer.

99 Bottles of Beer done in Prolog-languages

Prolog

Works with: SWI Prolog

<lang prolog>bottles(0):-!. bottles(X):-

   writef('%t bottles of beer on the wall \n',[X]),
   writef('%t bottles of beer\n',[X]),
   write('Take one down, pass it around\n'),
   succ(XN,X),
   writef('%t bottles of beer on the wall \n\n',[XN]),
   bottles(XN).
- bottles(99).</lang>

An other version that handles plural/not plural conditions.

<lang prolog>line1(X):- line2(X),write(' on the wall'). line2(0):- write('no more bottles of beer'). line2(1):- write('1 bottle of beer'). line2(X):- writef('%t bottles of beer',[X]). line3(1):- write('Take it down, pass it around'). line3(X):- write('Take one down, pass it around'). line4(X):- line1(X).

bottles(0):-!. bottles(X):-

   succ(XN,X),
   line1(X),nl,
   line2(X),nl,
   line3(X),nl,
   line4(XN),nl,nl,
   !,
   bottles(XN).

- bottles(99).</lang>