Thursday, November 19, 2009

How Does One Write Programs that Display or Print Themselves?

On 13th November 2009, I wrote on "Should Interviewers for Entry Level Programming Positions be "Trained" in Interviewing?" While the posting was about interviews, interviewers and out-of-the-ordinary questions, what appeared to have attracted the attention of student readers is the part about "writing programs that display or print themselves".

The most frequently asked question appears to be "How does one write programs that display or print themselves?" I will guide you to sites on the Internet that would help you learn how to do this.

A program that displays or prints itself is called a quine. This is named in the honor of Willard van Orman Quine, the American philosopher and logician. You can read about Quine and his contributions in Wikipedia.

I will describe at a very high level one possible method to implement a quine. The implementation of a quine consists of two distinct components. One of the components contains the definition of a string, and the other component is the program itself. The string itself contains the central part of the source program in the second component. The second component that contains the central program itself prints the string two times. The first time is to print the string definition. The second time is to print the central part of the program itself. This is the basic principle on which many quines are written. But writing the program itself could mean a lot of work.

Those of you who are really interested in learning how to write a quine must go through Igor Ostrovsky's blog on "How to write a self-printing program". Unfortunately, to understand this, you must be familiar with C#. At any rate, it does not hurt to read this well-written blog.

Over the years, many programmers have written quines in several languages. Quines are written in C, C++, Java, JavaScript, Perl, Python and several more. You must take a look at The Quine Page!

I am directing you to a few website that discusses quines to just satisfy your curiosity. If you do not understand how to write quines, do not be disheartened. If you had really gone through the Wikipedia, tried to understand what Igor Ostrovsky wrote about developing a quine in C#, and checked out The Quine Page , it would have been rewarding enough. But do not stop there, think of questions like "Are quines just oddities?" or "Do they have any useful applications?"

No comments:

Post a Comment