Thursday, September 25, 2008

Setting an example - "Hello World" , "99 bottles of beer" is here.

I've been terribly lazy , I've not posted anything o'er here in a long time and i badly want to write some crap :P , so.. here goes

"Hello World" , "99 bottles of beer" is here.

People who have been trained or have been training others in computer languages would definitely have come across this very famous sentence in the study material they use - the "Hello World" . A very simple sentence , but unmistakably the most popular example that is used to introduce people to a computer language.
As you read this... there might be several hundreds of budding programmers out there , typing this line and marking their foray into the world of programming. But how popular is this?

If the helloworld site is to be believed , there are 409 computer language versions ( actual code for all those is available on the site) of the most popular example program. Now , that is almost all the computer languages man has ever created.

A couple of them caught my attention :

1) the longest possible coding for a simple , single lined output

The RedCode Version (alias- the redneck version):

; Should work with any MARS >= ICWS-86
; with 128x64 gfx core
Start MOV 0,2455
MOV 0,2458
MOV 0,2459
MOV 0,2459
MOV 0,2459
MOV 0,2459
MOV 0,2459
MOV 0,2460
MOV 0,2465
MOV 0,2471
MOV 0,2471
MOV 0,2471
MOV 0,2479
MOV 0,2482
MOV 0,2484
MOV 0,2484
MOV 0,2484
MOV 0,2486
MOV 0,2486
MOV 0,2486
MOV 0,2486
MOV 0,2488
MOV 0,2493
MOV 0,2493
MOV 0,2493
MOV 0,2493
MOV 0,2497
MOV 0,2556
MOV 0,2559
MOV 0,2560
MOV 0,2565
MOV 0,2570
MOV 0,2575
MOV 0,2578
MOV 0,2585
MOV 0,2588
MOV 0,2589
MOV 0,2592
MOV 0,2593
MOV 0,2596
MOV 0,2597
MOV 0,2603
MOV 0,2605
MOV 0,2608
MOV 0,2667
MOV 0,2670
MOV 0,2671
MOV 0,2676
MOV 0,2681
MOV 0,2686
MOV 0,2689
MOV 0,2696
MOV 0,2699
MOV 0,2700
MOV 0,2703
MOV 0,2704
MOV 0,2707
MOV 0,2708
MOV 0,2714
MOV 0,2716
MOV 0,2719
MOV 0,2778
MOV 0,2778
MOV 0,2778
MOV 0,2778
MOV 0,2778
MOV 0,2779
MOV 0,2779
MOV 0,2779
MOV 0,2782
MOV 0,2787
MOV 0,2792
MOV 0,2795
MOV 0,2802
MOV 0,2805
MOV 0,2806
MOV 0,2809
MOV 0,2810
MOV 0,2810
MOV 0,2810
MOV 0,2810
MOV 0,2812
MOV 0,2818
MOV 0,2820
MOV 0,2823
MOV 0,2882
MOV 0,2885
MOV 0,2886
MOV 0,2891
MOV 0,2896
MOV 0,2901
MOV 0,2904
MOV 0,2911
MOV 0,2912
MOV 0,2913
MOV 0,2914
MOV 0,2917
MOV 0,2918
MOV 0,2919
MOV 0,2922
MOV 0,2928
MOV 0,2930
MOV 0,2933
MOV 0,2992
MOV 0,2995
MOV 0,2996
MOV 0,3001
MOV 0,3006
MOV 0,3011
MOV 0,3014
MOV 0,3021
MOV 0,3022
MOV 0,3023
MOV 0,3024
MOV 0,3027
MOV 0,3028
MOV 0,3030
MOV 0,3032
MOV 0,3038
MOV 0,3040
MOV 0,3103
MOV 0,3106
MOV 0,3107
MOV 0,3107
MOV 0,3107
MOV 0,3107
MOV 0,3107
MOV 0,3108
MOV 0,3108
MOV 0,3108
MOV 0,3108
MOV 0,3108
MOV 0,3109
MOV 0,3109
MOV 0,3109
MOV 0,3109
MOV 0,3109
MOV 0,3111
MOV 0,3111
MOV 0,3111
MOV 0,3120
MOV 0,3121
MOV 0,3124
MOV 0,3124
MOV 0,3124
MOV 0,3126
MOV 0,3129
MOV 0,3130
MOV 0,3130
MOV 0,3130
MOV 0,3130
MOV 0,3130
MOV 0,3131
MOV 0,3131
MOV 0,3131
MOV 0,3131
MOV 0,3135
JMP 0

(Imagine having to program with this assembly language . Guaranteed to give any sane geek the creeps. )

And 2) The shortest code :

The APL version (alias - the K.I.S.S version. All hail the APL) :

'Hello, world!'

//thats it folks , as simple as that!!

However , there seems to be another example .. which is a bit advanced than the "hello world". Its called "99 bottles of beer".

It happens to be a song that kids sing .

WTF?.... why in the world didnt i know about that song?

What might be the reasons I missed the song?

These might have been the reactions when the proposition to include the song in our computer syllabus at school had come up.

"BEER SONG FOR KIDS ?? :O Kids shouldnt sing about beer , definitely not my kid.. that is blasphemy, thats anti-social , thats totally unacceptable behavior. " - my parents.

"blah blah blah blah .. BEER is evil. It makes you the satan's slave and also blah blah blah" - my neighbor.

"Singing/Coding the song makes you feel like you want to drink beer. This is a scientific conclusion" - NCERT / CBSE / TN State Board.

Anyway .. I doubt whether that song wouldnt have made it to our text books. Here is the lyrics :

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

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

97 bottles of beer on the wall, 97 bottles of beer.
Take one down and pass it around, 96 bottles 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."

Now , why is this an advanced example? This example can be used to demonstrate the printing statement syntax, a loop control , a branching statement and the ill-effects of beer on your heads. Psychotic geeks-in-the-making can be identified if he/she makes the above output a continuous , infinite loop , virtually implying infinite beer .

Apparently , this example has more takers than the lame "Hello World". According to 99 bottles of beer dot net there are 1229 representations of the beer song in different languages and their variants.

My 4 line C program :P

main()
{
int i;
for (i=99;i>=2;printf("%d bottles of beer on the wall, %d bottles of beer.\n Take one down and pass it around,%d bottles of beer on the wall.\n",i,i,i-1),i--);
printf("1 bottle of beer on the wall, 1 bottle of beer.\n Take one down and pass it around,no more bottles of beer on the wall.\n");
printf("No more bottles of beer on the wall, no more bottles of beer.\n Go to the store and buy some more,99 bottles of beer on the wall.\n");
}

Got a better code for the problem above ? Post it on the comments.

When I went thro the lyrics.. I had two questions in my mind ..

1) Were the kids made to sing all the way from number 99 to 0 beer bottles? If the answer is "YES" , that answers my question " Why is that almost everyone in the West drink beer? "

2)What would be the RedCode version of this program ? :O :D (Am sure I'd go retarded if I tried coding the same in RedCode.)