2009-02-27

programming can be fun (polyglot introduction)

programming can be boring, especially when you hit deadlines, or when you have to track heisenbugs.

but it can (and should) be fun. it can also be seen as a form of art - don't laugh, i'm serious. i'll try to show this through the following posts, where i'm going to write a polyglot program, while explaining the tricks i'm using to get it working.

a polyglot program is a computer program that can be understood by different compilers / interpreters, performing the same operations independently of the programming language used. it's a single file, where one tries to stuff as many different programming languages as possible.

before you start grumbling that this is not useful in everyday life, let me state that i am aware of this fact. :-) those posts are only here to have some fun and show you what can be done with programming languages, in ways that were not really foreseen when the languages were being defined. repeat after me: those posts are not meant to be a programming guide.

so, why would i write such a beast? well, the simplest reason is because i can. because it's fun. and there's not much more to it... if you think it's a moronic thing to do, i'm not forcing you to read them. after all, you came in here! :-)

in order to spice up the game, this program will do more than just say hello to the world. yup, this program will be a real one, that computes the first 10 numbers of the fibonacci sequence and display them on the screen. i said "computes", which means following the real algorithm, not displaying a stored sequence...

here's the perl version for this algorithm. it will be our base upon which we build, adding more languages:

$i = 0;
$n1 = $n2 = 1;
print "$n1\n";
while ( $i < 9 ) {
($n1, $n2) = ($n2, $n1+$n2);
print "$n1\n";
$i++;
}


note: i know that it's not really perlish, but it will allow us for a better integration with c.

nothing fancy in this program, running under plain perl 5.10 - but without strict and warnings. i won't go into much details regarding the algorithm. once again, it's really a straightforward translation of the fibonacci algorithm.

this post was just an introduction, stay tuned for the next steps. following posts will be labelled polyglot, for easier reference to the complete story.

2 comments:

  1. i want to download mandriva 2009.1(spring (free)) you can tell me how to burn it into a dvd?

    ReplyDelete
  2. @levi: please post your comments on a relevant blog post.
    you'd better download 2010.0 which is out since november, available at http://www2.mandriva.com/downloads/
    then, any burning soft will do (k3b, xcdroast, etc.)

    ReplyDelete