Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Quine - A program that computes & prints its own source code


Published on

A simple way to write a program that *computes* and outputs its own source code.

The actually program itself is not posted but the entire implementation idea is explained - hopefully this encourages people to write the program on their own.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Quine - A program that computes & prints its own source code

  1. 1. Write a program that prints itself<br />By Kiran Bhogadi<br /><br />
  2. 2. One simple rule<br />Don’ts :<br /> The program can not read its own source code file and output it.<br />Do’s :<br /> The program is set up to compute its own source code and output it.<br />
  3. 3. A preliminary approach<br />Divide program into 2 parts :<br />A and B<br />A<br />B<br />Program =<br />
  4. 4. A preliminary approach<br />In your program :<br />A and B could be two different functions/methods<br />Or A and B could each be just a line of code<br />Assume that execution starts in A and ends with B<br />START END<br />A<br />B<br />
  5. 5. A First Implementation Idea <br />A and B are two methods<br />The method A() has a copy of method B()’s code (as a literal string). A() prints this string and then calls B()<br />A<br />“<B>”<br />
  6. 6. A First Implementation Idea (contd.)<br />Tempting to design method B() to contain the code of method A as a literal string too! <br />But that won’t work!<br />Circular Dependency<br />
  7. 7. A better approach<br />Method A has a string literal copy of Method B code and it simply prints it. <br />Method B will compute the<br /> string the represents Method A’s code<br />A<br />“<B>”<br />
  8. 8. Method A’s code – contains Method B as a string literal<br />Let’s say the method A is written like this :<br />Method A () {<br />Print(“<Code_for_Method_B>”)<br />}<br />
  9. 9. Can we compute the code for Method A … <br /> … given the code in the “box” i.e., “<Code_for_Method_B> ?<br />YES!!<br />
  10. 10. Computing Method A’s code given <Code_for_Method_B> as input<br />The code (method) that computes Method A’s code :<br />Input_String S For e.g. “<Code_for_Method_B>”<br />Returns :<br />“Method A () {” + NEWLINE<br /> + “Print(“” + Input_String S + “”)” + NEWLINE<br /> + “}”<br />Note the returned string represents Method A’s code!<br />Method A () {<br />Print(“<Code_for_Method_B>”)<br />}<br />
  11. 11. So how does the code for Method B look like?<br />See the previous slide!<br />
  12. 12. Putting it All together<br />Method A () {<br /> Print(<Code_for_Method_B>);<br />Call B(…);<br />}<br /><Code_for_Method_B>is a string literal representing Method B’s code :<br />Method B(Input String S) {<br /> String returnValue = <br /> “Method A () {” + NEWLINE<br /> + “Print(” + Input_String S + “);” + NEWLINE<br /> + “Call B(…);” + NEWLINE<br /> + “}”;<br />Print returnValue;<br />}<br />
  13. 13. What happens when the program runs ?<br />Execution starts with Method A()<br />A() outputs the code (“literal”) for Method B<br />A() calls B()<br />B() computes the code for Method A and outputs it<br />Method A()Method B()<br />Step 2: calls<br />Step 3 : outputs computed value<br />Step 1 : outputs literal<br />B<br />A<br />
  14. 14. Some Technicalities / Questions<br />If the original program file contained the methods A and B in this order:<br /> then the output from the program contained the methods in reverse order<br />But this can be fixed easily!<br />A<br />B<br />B<br />A<br />
  15. 15. Further Reading<br /><ul><li>Michael Sipser- Introduction to the Theory of Computation (</li>