99 Bottles of Beer/Prolog: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Fixed syntax highlighting and duplicate headers.)
 
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}}==
===Prolog===
{{works with|SWI Prolog}}
{{works with|SWI Prolog}}
<lang prolog>bottles(0).
<syntaxhighlight 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 21: Line 18:
bottles(XN).
bottles(XN).


:- bottles(99).</lang>
:- bottles(99).</syntaxhighlight>


It is possible to extend the previous version so to handle singular and plurals of the word 'bottle':
It is possible to extend the previous version so to handle singular and plurals of the word 'bottle':


<lang prolog>bottles(0):- write('no more bottles of beer').
<syntaxhighlight lang="prolog">bottles(0):- write('no more bottles of beer').
bottles(B) :-
bottles(B) :-
dif(B,0), NewB is B - 1,
dif(B,0), NewB is B - 1,
Line 40: Line 37:
bottles(NewB).
bottles(NewB).


:- bottles(99).</lang>
:- bottles(99).</syntaxhighlight>


Another version that handles plural/not plural conditions.
Another version that handles plural/not plural conditions.


<lang prolog>line1(X):- line2(X),write(' on the wall').
<syntaxhighlight lang="prolog">line1(X):- line2(X),write(' on the wall').
line2(0):- write('no more bottles of beer').
line2(0):- write('no more bottles of beer').
line2(1):- write('1 bottle of beer').
line2(1):- write('1 bottle of beer').
Line 62: Line 59:
bottles(XN).
bottles(XN).
:- bottles(99).</lang>
:- bottles(99).</syntaxhighlight>


=={{header|Visual Prolog}}==
===Visual Prolog===
<lang visual prolog>
<syntaxhighlight lang="visual prolog">
implement main
implement main
open core, std, console
open core, std, console
Line 89: Line 86:


goal
goal
mainExe::run(main::run).
mainExe::run(main::run).</syntaxhighlight>
</lang>

Latest revision as of 20:51, 1 September 2022

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
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).

It is possible to extend the previous version so to handle singular and plurals of the word 'bottle':

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).

Another version that handles plural/not plural conditions.

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).

Visual Prolog

implement main
    open core, std, console

class predicates
    bottles : (integer) -> string procedure (i).

clauses
    bottles(1) = "bottle" :- !.
    bottles(_) = "bottles".

    run():-
        init(),
        foreach B = downTo(99,1) do
            write(B," ",bottles(B), " of beer on the wall,\n"),
            write(B," ",bottles(B), " of beer,\n"),
            write("Take one down, pass it around,\n"),
            write(B-1," ",bottles(B-1)," of beer on the wall.\n\n")
        end foreach,

        succeed(). 
end implement main

goal
    mainExe::run(main::run).