99 bottles of … You gotta be kidding me

I was reading Lambda the Ultimate and someone posted they had a program accepted by http://99-bottles-of-beer.net/. My reaction: a combination of “please tell me you are joking” and WTF?. But it is for real, over 1,400 programs in different languages that print out lyrics to 99 bottles of beer on the wall. If you are a language geek, it is worth looking at the “Top List”, there are some amazing stuff there, like Piet, a language of color, where the programs look like abstract art (no text). My favorite, of the “just show me some code” solutions is Haskell, it really shows the power of pattern matching: it is short, concise and clear.

99 bottles seems like it should be one of the most trivial programs but there a couple of things that make it “interesting”. Here are the last verses:

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, 99 bottles of beer on the wall.

Here is my solution:

[99..0,-1].apply2(fcn(n){
   println(beers(n), ” on the wall, “, beers(n).toLower(),
      “.\n”,
      n==0 and
      (“Go to the store and buy some more, 99 bottles of beer”)
      or (“Take one down and pass it around, ” +
         beers(n-1).toLower()),
      ” on the wall.\n”)
});
fcn beers(n){
    (n==0 and “No more bottles” or (n==1 and
    “1 bottle” or “” + n + ” bottles”))
    + ” of beer”
}

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s