Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Icon > 99 Bottles of B...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 108 of 181
Post > Topic >>

99 Bottles of Beer

by "Frank J. Lhota" <NOSPAM.lhota@[EMAIL PROTECTED] > Jan 26, 2006 at 08:45 PM

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




 1 Posts in Topic:
99 Bottles of Beer
"Frank J. Lhota"  2006-01-26 20:45:11 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue May 13 14:47:15 CDT 2008.