99 Bottles of Beer/Prolog: Difference between revisions
m
Fixed syntax highlighting and duplicate headers.
(moving code from main task-page to sub-page / Visual Prolog) |
m (Fixed syntax highlighting and duplicate headers.) |
||
(One intermediate revision by one other user not shown) | |||
Line 4:
{{collection|99 Bottles of Beer}}
[[99 Bottles of Beer]] done in Prolog-languages
__toc__
==
{{works with|SWI Prolog}}
<
bottles(X):-
writef('%t bottles of beer on the wall \n',[X]),
Line 21 ⟶ 18:
bottles(XN).
:- bottles(99).</
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.▼
<
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).</syntaxhighlight>
<syntaxhighlight 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').
Line 43 ⟶ 59:
bottles(XN).
:- bottles(99).</
==
<
implement main
open core, std, console
Line 70 ⟶ 86:
goal
mainExe::run(main::run).</syntaxhighlight>
|