2006-03-04

The programming: 99 bottles

Oh, amazing site - www.99-bottles-of-beer.net It collects a great many programs in different languages printing plain and simple song:


99 bottles of beer on the wall, 99 bottle of beer!
Take one down, and pass it around. 98 bottles of beer on the wall.

98 bottles of beer on the wall, 99 bottle of beer!
Take one down, and pass it around. 97 bottles of beer on the wall.

...


What languages there are not here: ANT, J, K, CLIPS, Erang, Ocaml, 1C, naturally there are C/C++, Java, Perl.

Some exoteric languages made me smile - Ook! (This is for those who read Pratchett),
Shakespeare Programming Language (at least a brief acquaintance with works of the author with the same name is also recommended), Multithreaded Brainf * ck, procmail.

But as the most beautiful solution I unexpectedly chose a program in Refal.

With the printing the beginning of the song all languages cope pretty enough. The simple end spoils everything:

...

2 bottles of beer on the wall, 2 bottle 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 bottles of beer on the wall, No more bottles bottles of beer!
Go to store and buy some more. 99 of beer on the wall.


In some languages code is short, simple and clear but either incorrectly declines «1 bottles», or gladly reports in the end «0 bottles of beer on the wall». After fixing it becomes huge and confusing.

ANT by using the extensions package ant-contrib demonstrates the all ugliness of XML for recording consecutive actions.

But 1C code looks rather good due to the presence of operator ? in the language. If there is no petty operator + it would be not so bad illustration of imperative approach for solving this problem.

Excel VBA wonderfully demonstrates what terror can come out of it if do not separate logic from visual appearance.

Common Lisp code made me smile — the only program, which actually does what was requested — printing the song. In the language standard there is 'format' function (highly advanced analogue of C printf) its abilities are pretty enough for this song :).

Program in Scheme generates the required string in compile time. There there is a terrifying realization and quite good one.

C++ solution with using templates amazes by its laconism and demonstrates a simple rule-based approach in compile time.

Rule-based forward chaining language CLIPS fits successfully. It's a little bit verbose, but then easy extended.

For some reason neither JESS nor Drools solutions are present. Aren't they unpopular?

Java Exception oriented programming is the joke of the month! throw new BottleExeption(1), and redefine printStackTrace() method %)

Refal is great! Pattern matching is a wonderful tool for the decomposition.

I expected to see something similar in OCalm, but in the beginning they build recursive variant type isomorphic to the song verses and after that the substitution is going on. I have not understood yet either it is an awfully overhead, or a remarkable approach, using effectively the advantages of compile time and run time.

At the same site there is a lot of programs written in different Assembler dialects, Basic and SQL, but they are not so interesting for me.

No comments:

Post a Comment