99 bottles of beer: Difference between revisions

(Fixed missing / in closing tag)
Tags: Mobile edit Mobile web edit
(48 intermediate revisions by 18 users not shown)
Line 215:
cl_demo_output=>display( ).
</syntaxhighlight>
 
=={{header|ABC}}==
<syntaxhighlight lang="abc">HOW TO RETURN bottles n:
SELECT:
n<0: RETURN "99 bottles"
n=0: RETURN "No more bottles"
n=1: RETURN "1 bottle"
n>1: RETURN "`n` bottles"
 
HOW TO SING VERSE n:
WRITE "`bottles n` of beer on the wall,"/
WRITE "`bottles n` of beer,"/
SELECT:
n=0: WRITE "Go to the store and buy some more,"/
n=1: WRITE "Take it down and pass it around,"/
n>1: WRITE "Take one down and pass it around,"/
WRITE "`bottles (n-1)` of beer on the wall."/
WRITE /
 
FOR n IN {0..99}:
SING VERSE 99-n</syntaxhighlight>
 
=={{header|ACL2}}==
See [[99 Bottles of Beer/Lisp]]
 
=={{header|Acornsoft Lisp}}==
See [[99 Bottles of Beer/Lisp]]
 
Line 956 ⟶ 980:
 
-- At the prompt, type 'N beer !' (no quotes), where N is the number of stanzas you desire</syntaxhighlight>
 
=={{header|BabyCobol}}==
<syntaxhighlight lang="cobol">
* Pointing out some interesting things:
* - BY 0 subclause of VARYING (illegal in some COBOL dialects)
* - PERFORM THROUGH with internal/external GO TOs
* - using non-reserved keywords (END, DATA)
* - ALTER (works the same way in COBOL)
* - fall-through from MANY-BOTTLES
* - the last NEXT SENTENCE does nothing (plays the role of EXIT)
IDENTIFICATION DIVISION.
PROGRAM-ID. 99 BOTTLES.
DATA DIVISION.
01 DATA PICTURE IS 999.
PROCEDURE DIVISION.
LOOP VARYING DATA FROM 99 BY 0
PERFORM COUNT-BOTTLES THROUGH END
DISPLAY DATA "bottles of beer"
DISPLAY "Take one down, pass it around"
SUBTRACT 1 FROM DATA
IF DATA = 1
THEN ALTER COUNT-BOTTLES TO PROCEED TO SINGLE-BOTTLE
END
PERFORM COUNT-BOTTLES THROUGH END
DISPLAY ""
END.
NO-BOTTLES-LEFT.
DISPLAY "No bottles of beer on the wall"
DISPLAY ""
DISPLAY "Go to the store and buy some more"
DISPLAY "99 bottles of beer on the wall".
STOP.
COUNT-BOTTLES.
GO TO MANY-BOTTLES.
SINGLE-BOTTLE.
DISPLAY DATA "bottle of beer on the wall".
GO TO NO-BOTTLES-LEFT.
MANY-BOTTLES.
DISPLAY DATA "bottles of beer on the wall".
END.
NEXT SENTENCE.
</syntaxhighlight>
 
=={{header|BASIC}}==
Line 1,442 ⟶ 1,508:
p "One bottle of beer on the wall, one bottle of beer!"
p "Take one down, pass it around, no more bottles of beer on the wall."</syntaxhighlight>
 
=={{header|Bruijn}}==
<syntaxhighlight lang="bruijn">
:import std/Combinator .
:import std/Number .
:import std/String .
 
main [y [[=?0 case-end case-rec]] (+99)]
case-rec n ++ t1 ++ n ++ t2 ++ t3 ++ n ++ t1 ++ "\n" ++ (1 --0)
n number→string 0
t1 " bottles of beer on the wall\n"
t2 " bottles of beer\n"
t3 "Take one down, pass it around\n"
case-end empty
</syntaxhighlight>
 
=={{header|BQN}}==
Line 1,943 ⟶ 2,024:
WriteLine("Go to the store to buy some more, 99 bottles of beer on the wall...");
}</syntaxhighlight>
 
=={{header|C/vFP16}}==
<syntaxhighlight
lang="cpp">#pragma SCH_64_16_IFP
#import <jobsched.c>
 
__attr(@canschedule)
volatile constricts async UVOID <__INVAR const T>base_000000 __PCON(
impure VFCTX_t^ ct_base,
impure MCHR_t^ sched_arg,
__LVAR <const T>XVF_fntype_t^ f,
<out VF_QSWitch_t>XVF_fn_t^ switchctx
) __ARGFILL {
VF_Xsched_ILock(ct_base, $->sched);
 
captured __VFObj <>VF_KeDbg^ ki = VF_Xg_KeDbg_Instance();
 
captured deferred <__VF_T_Auto>__VF_Auto^ vfke = copyof VF_KeDbg_GetRstream<MCHR_t^>(captures ki);
VF_Gsched_SOBFree(sched_arg);
VF_Gsched_Alloc_U16(65535);
VF_Msched_MChr_lim(1496);
VF_Osched_Begin();
VF_Fsched_Add2(%beer_000099);
 
VF_Xsched_IUnlock(ct_base, $->sched);
$switchctx(IMPURE_CTX, %beer_000099(%vfke));
}
 
__attr(@eUsesIo)
impure constricts UVOID synchronized beer_000099(
impure __noinline <MCHR_t^>RIGHTSTREAM_t^ outrs
) __NFILL {
pure UVOID^ uvid = __attr(@purify) $UVOID.;
while ( 99 > ((volatile NUM_t)^(NUM_t^)(uvid))++ ) {
VF_STM_Out_NUM((NUM_t^)uvid);
VF_STM_Out_x(__Dynamic C"bottles on the wall.\nPut one down, pass it around\n");
}
return $__;
}
</syntaxhighlight>
 
=={{header|C++}}==
Line 3,705 ⟶ 3,829:
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func$ checkPluralbottle num . word$ .
if num = 1
word$ =return "bottle"
else
word$ = "bottles"
.
return "bottles"
.
#
for i = 99 step -1 to 1
repeat
call checkPlural i pluralWord$
print i & " " & pluralWord$bottle i & " of beer on the wall"
print i & " " & pluralWord$bottle i & " of beer"
print "Take one down, pass it around"
call checkPlural i -= 1 pluralWord$
ifuntil i - 1 = 0
print i & print" "No more& bottlesbottle i & " of beer on the wall"
else
print i - 1 & " " & pluralWord$ & " of beer on the wall"
.
print ""
.
print "No more bottles of beer on the wall"
</syntaxhighlight>
 
Line 3,755 ⟶ 3,876:
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">module Bottles {
void run() {
@Inject Console console;
void run()
{
function String(Int) num = i -> i==0 ? "No" : i.toString();
function String(Int) bottles = i -> i==1 ? "bottle" : "bottles";
 
for@Inject (IntConsole remain : 99..1)console;
for (Int remain : 99..1) {
console.print($|{num(remain)} {bottles(remain)} of beer on the wall
|{num(remain)} {bottles(remain)} of beer
Line 3,771 ⟶ 3,889:
|
);
}
}
}</syntaxhighlight>
}</syntaxhighlight>
 
=={{header|Egel}}==
Line 3,964 ⟶ 4,082:
 
=={{header|Elena}}==
ELENA 56.0 :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 3,975 ⟶ 4,093:
= self.toPrintable() + (self != 1).iif(" bottles"," bottle");
bottleEnumerator() = new Variable(self).doWith::(n)
{
^ new Enumerator
Line 3,981 ⟶ 4,099:
bool next() = n > 0;
get Value() = new StringWriter()
.printLine(n.bottleDescription()," of beer on the wall")
.printLine(n.bottleDescription()," of beer")
.printLine("Take one down, pass it around")
.printLine((n.reduce:(1)).bottleDescription()," of beer on the wall");
reset() {}
enumerable() = __targetweak self;
}
};
Line 3,998 ⟶ 4,116:
var bottles := 99;
bottles.bottleEnumerator().forEach:(printingLn)
}</syntaxhighlight>
{{out}}
Line 4,083 ⟶ 4,201:
(message "%d bottles of beer on the wall" (1- i))
(setq i (1- i))))</syntaxhighlight>
 
=={{header|EMal}}==
This version writes the lyrics as described here: https://99-bottles-of-beer.net/lyrics.html
<syntaxhighlight lang="emal">
type NinetynineBottles
int DEFAULT_BOTTLES_COUNT = 99
model
int initialBottlesCount, bottlesCount
new by int =bottlesCount
me.initialBottlesCount = bottlesCount
end
fun subject = <|when(me.bottlesCount == 1, "bottle", "bottles")
fun bottles = <|when(me.bottlesCount == 0, "no more", text!me.bottlesCount)
fun goToWall = void by block
text line = me.bottles() + " " + me.subject() + " of beer on the wall, " +
me.bottles() + " " + me.subject() + " of beer."
if me.bottlesCount == 0 do line[0] = line[0].upper() end # text can be modified
writeLine(line)
end
fun takeOne = logic by block
if --me.bottlesCount < 0 do return false end # cannot take a beer down
writeLine("Take one down and pass it around, " + me.bottles() +
" " + me.subject() + " of beer on the wall.")
writeLine()
return true
end
fun goToStore = void by block
writeLine("Go to the store and buy some more, " + me.initialBottlesCount +
" bottles of beer on the wall.")
end
fun play = void by block
for ever
me.goToWall()
if not me.takeOne()
me.goToStore()
break
end
end
end
end
NinetynineBottles(when(Runtime.args.length > 0, int!Runtime.args[0], DEFAULT_BOTTLES_COUNT)).play()
</syntaxhighlight>
The output for two bottles of beer is as follows.
{{out}}
<pre>
2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.
 
1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around, no more bottles of beer on the wall.
 
No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 2 bottles of beer on the wall.
</pre>
 
=={{header|Erlang}}==
Line 4,833 ⟶ 5,005:
od;
end;</syntaxhighlight>
 
=={{header|GDScript}}==
{{works with|Godot|4.0}}
 
<syntaxhighlight lang="gdscript">
extends MainLoop
 
# Represents a count of bottles
class Bottles:
var count := 99
 
func take(n: int = 1) -> void:
count -= n
 
func _to_string() -> String:
match count:
0: return "No more bottles"
1: return "1 bottle"
_: return "%s bottles" % count
 
func _process(_delta: float) -> bool:
var bottles := Bottles.new()
while bottles.count > 0:
print("%s of beer on the wall" % bottles)
print("%s of beer" % bottles)
print("Take one down, pass it around")
bottles.take()
print("%s of beer on the wall" % bottles)
# Seperate paragraphs
if bottles.count > 0:
print()
 
return true # Makes the program exit
</syntaxhighlight>
 
===Silly node-tree version===
This uses the node's children as the display method (which can be viewed in-editor with the remote tab).
 
<syntaxhighlight lang="gdscript">
extends Node
 
@export var alcoholism: int = 99
 
func _ready():
# Add the lyrics as child nodes
var padding := "" # Avoid name clashes by adding spaces
for bottleCount in range(alcoholism, 0, -1):
# Seperate paragraphs with blank nodes
if bottleCount < alcoholism:
add_lyric(padding)
add_lyric("%s of beer on the wall" % [_formatBottles(bottleCount)])
add_lyric("%s of beer" % [_formatBottles(bottleCount)])
add_lyric("Take one down, pass it around" + padding)
add_lyric("%s of beer on the wall " % [_formatBottles(bottleCount - 1)]) # Extra space for name clash avoidance
padding += " " # Add spaces so the names don't clash
 
func _formatBottles(bottleCount: int) -> String:
return "%d bottle%s" % [bottleCount, "" if bottleCount == 1 else "s"]
 
func add_lyric(lyric: String) -> void:
var new_child := Node.new()
new_child.name = lyric
add_child(new_child)
</syntaxhighlight>
 
=={{header|Genie}}==
Line 6,048 ⟶ 6,284:
2 bottles of beer on the wall, 2 bottles of beer; take one down and pass it around, 1 bottle of beer on the wall
1 bottle of beer on the wall, 1 bottle of beer; take one down and pass it around, 0 bottles of beer on the wall </pre>
 
=={{header|Jakt}}==
<syntaxhighlight lang="jakt">
fn bottle_string(bottle_count: i64) throws -> String => match bottle_count {
0 => "No more bottles"
1 => "1 bottle"
else => format("{} bottles", bottle_count)
}
 
fn main() {
mut bottle_count = 99
while bottle_count > 0 {
println("{} of beer on the wall", bottle_string(bottle_count))
println("{} of beer", bottle_string(bottle_count))
println("Take one down, pass it around")
bottle_count--
println("{} of beer on the wall", bottle_string(bottle_count))
 
if bottle_count > 0 {
println("")
}
}
}
</syntaxhighlight>
 
=={{header|Janet}}==
Line 7,982 ⟶ 8,242:
end
</syntaxhighlight>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = map (Stdout . verse) [99, 98 .. 1]
 
verse :: num->[char]
verse n = lay [bottles n ++ " of beer on the wall,",
bottles n ++ " of beer,",
"Take " ++ pronoun ++ " down and pass it around,",
bottles (n-1) ++ " of beer on the wall!",
[]]
where pronoun = "it", if n=1
= "one", otherwise
 
bottles :: num->[char]
bottles n = "No more bottles", if n=0
= "1 bottle", if n=1
= (show n) ++ " bottles", otherwise</syntaxhighlight>
 
=={{header|mIRC Scripting Language}}==
Line 8,816 ⟶ 9,094:
70 PRINT BOTTLES-1 BOTTLES$ WALL$
80 NEXT</syntaxhighlight>
 
=={{header|Nutt}}==
<syntaxhighlight lang="Nutt">
module main imports native.io.output.say
 
for i|->{1,2..99;<|>) do
say(""+i+" bottles of beer on the wall, "+i+" bottles of beer")
say("Take one down and pass it around, "+(i-1)+" bottles of beer on the wall.")
done
 
end
</syntaxhighlight>
 
=={{header|OASYS}}==
Line 11,305 ⟶ 11,595:
bottles n - 1 "of beer on the wall" crlf
]]</syntaxhighlight>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Verses 99>>;
};
 
Verses {
'-'1 = ;
s.1 = <Verse s.1>
<Verses <- s.1 1>>;
};
 
Verse {
s.1 = <Bottles s.1> ' of beer on the wall,\n'
<Bottles s.1> ' of beer,\n'
<ThirdLine s.1> '\n'
<Bottles <- s.1 1>> ' of beer on the wall!\n\n';
};
 
Bottles {
'-'1 = '99 bottles';
0 = 'No more bottles';
1 = '1 bottle';
s.1 = s.1 'bottles';
};
 
ThirdLine {
0 = 'Go to the store and buy some more,';
1 = 'Take it down and pass it around,';
s.1 = 'Take one down and pass it around,';
};</syntaxhighlight>
 
=={{header|Relation}}==
Line 11,580 ⟶ 11,901:
</syntaxhighlight>
 
=={{header|RPL}}==
≪ " bottles" " of beer" " on the wall" "Take one down and pass it around,"
→ n b of on take
≪ n 1 '''FOR''' j
j →STR b + of + DUP on + "," + 1 DISP
"," + 2 DISP
take 3 DISP
j 1 ≠ LAST - →STR "No more" IFTE
b +
'''IF''' j 2 == '''THEN''' 1 OVER SIZE 1 - SUB '''END'''
of + on + "." + 4 DISP
-1 '''STEP'''
≫ ≫ '<span style="color:blue">BOB</span>' STO
 
99 <span style="color:blue">BOB</span>
=={{header|RPL/2}}==
===Simple solution===
Line 12,262 ⟶ 12,598:
(v ~~ 0) ifTrue: [ Transcript show: (sr at:4) ; cr. ].
].</syntaxhighlight>
 
 
This version uses Squeak Smalltalk's String >> format: method. and SequencableCollection >> atPin: method
<syntaxhighlight lang="smalltalk">
|bottles plurals |
Transcript clear.
bottles:='{1} bottle{2} of beer on the wall
{1} bottle{2} of beer
Take one down, pass it around
{3} bottle{4} of beer on the wall'.
plurals := #('' 's').
99 to: 1 by: -1 do:[:v |
Transcript show: (bottles format: {(v asString) . (plurals atPin:v) . ((v -1) asString). (plurals atPin:v) }); cr; cr].
 
Transcript show: 'hic!'; cr.
 
</syntaxhighlight>
 
 
This version uses Squeak Smalltalk's String >> asPluralBasedOn: method to handle the plural and singular cases.
<syntaxhighlight lang="smalltalk">
 
| bottles |
Transcript clear.
bottles := '{1} {2} of beer on the wall
{1} {2} of beer
Take one down, pass it around
{3} {4} of beer on the wall'.
99 to: 1 by: -1 do: [:i |
Transcript
show: (bottles format: {
i. 'bottle' asPluralBasedOn: i.
i - 1. 'bottle' asPluralBasedOn: i - 1});
cr; cr].
Transcript show: 'hic!'; cr.
</syntaxhighlight>
 
=={{header|SmileBASIC}}==
Line 13,076 ⟶ 13,448:
 
main = whole_song 99</syntaxhighlight>
 
=={{header|Uxntal}}==
<syntaxhighlight lang="Uxntal">( uxncli 99bottles.rom )
 
|10 @Console &vector $2 &read $1 &pad $4 &type $1 &write $1 &error $1
 
|0100 ( -> )
#63 &loop
DUP <print-verse>
[ LIT2 0a -Console/write ] DEO
#01 EQUk ?&done
POP #01 SUB
!&loop
&done BRK
 
@<print-verse> ( num -- )
DUP <print-bottle> ;dict/wall <print-string>
DUP <print-bottle> [ LIT2 0a -Console/write ] DEO
;dict/take <print-string>
#01 SUB <print-bottle> ;dict/wall !<print-string>
 
@<print-bottle> ( num -- )
DUP #00 EQU ?&zero
DUP #01 EQU ?&one
<print-dec> ;dict/bottle <print-string>
[ LIT2 "s -Console/write ] DEO
!&end
 
&one ( num -- )
<print-dec> ;dict/bottle <print-string>
!&end
&zero ( num -- )
POP ;dict/no-more <print-string>
;dict/bottle <print-string>
[ LIT2 "s -Console/write ] DEO
( >> )
&end
;dict/of-beer
( >> )
 
@<print-string> ( str -- )
&loop
LDAk .Console/write DEO
INC2 LDAk ?&loop
POP2 JMP2r
 
@<print-dec> ( byte -- )
DUP #64 DIV <print-num>/try
DUP #0a DIV <print-num>/try
( >> )
 
@<print-num> ( num -- )
#0a DIVk MUL SUB
[ LIT "0 ] ADD .Console/write DEO
JMP2r
 
&try ( num -- )
DUP ?<print-num>
POP JMP2r
 
@dict &no-more "No 20 "more $1
&bottle 20 "bottle $1
&of-beer 20 "of 20 "beer 20 $1
&wall "on 20 "the 20 "wall 0a $1
&take "Take 20 "one 20 "down, 20 "pass 20 "it 20 "around 0a $1</syntaxhighlight>
 
=={{header|UTFool}}==
Line 13,431 ⟶ 13,868:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">for (i in 99...0) {
System.print("%(i) bottles of beer on the wall,")
System.print("%(i) bottles of beer,")
System.print("Take one down, pass it around,")
System.print("%(i - 1) bottles of beer on the wall.\n")
}</syntaxhighlight>
 
Line 13,715 ⟶ 14,152:
</syntaxhighlight>
 
 
=={{header|YAMLScript}}==
<syntaxhighlight lang="yaml">
#!/usr/bin/env ys-0
 
# Print the verses to "99 Bottles of Beer"
#
# usage:
# ys 99-bottles.ys [<count>]
 
defn main(number=99):
each [n (number .. 1)]:
say: paragraph(n)
 
defn paragraph(num): |
$bottles(num) of beer on the wall,
$bottles(num) of beer.
Take one down, pass it around.
$bottles(num - 1) of beer on the wall.
 
defn bottles(n):
cond:
n == 0 : 'No more bottles'
n == 1 : '1 bottle'
=> : "$n bottles"
</syntaxhighlight>
 
=={{header|Yorick}}==
3

edits