The site
http://www.99-bottles-of-beer.net
presents algorithms for writing out the lyrics to the song "99 Bottles
of Beer" in hundreds of different languages. There is an Icon entry,
which attracted one reader comment. The reader complained that the Icon
example was boring. "This is BASIC written in Icon" is the way the
reader put it.
I have an alternate Icon implementation for "99 bottles" that better
illustrates the power of the language. This version has the following
advantages:
- Numbers are spelled out (e.g. "Ninety-nine");
- The IPL is used (the numerics procedure spell);
- It includes a string scan; and
- It writes out the song's final verse ("Go to the store...").
The code for this version is as follows. Feel free to critique and / or
improve.
# Frank J. Lhota
link numbers # using spell procedure
#################################################################
#
# Returns a phrase indicating the beer inventory of the wall
#
#################################################################
procedure inventory(beercount)
static lc, uc
initial {
lc := string (&lcase)
uc := string (&ucase)
}
return case beercount of {
0 : "No more bottles"
1 : "One bottle"
default : {
# Capitalize the result of spell
spell(beercount) ? (
map (move (1), lc, uc) || tab (0) || " bottles" )
}
} || " of beer on the wall"
end
#################################################################
#
# Writes out the verse for beercount number of beers. Returns the
# number of beers on the wall at the end of the verse
#
#################################################################
procedure write_verse (beercount)
local inv
write (inv := inventory(beercount), ",")
write (inv, ".")
if beercount > 0 then {
write ("Take one down and pass it around,")
write (inventory(beercount -:= 1), ".")
}
else {
write ("Go to the store and buy some more,")
write (inventory(beercount := 99), ".")
}
write()
return beercount
end
procedure main()
local beercount
# Start with 99 bottles;
# Keep writing verses until the wall is restocked
beercount := 99
until (beercount := write_verse (beercount)) = 99
return
end


|