Monday 28 November 2016

Quines

Reproduction is no walk in the park. It takes unmatched effort or maybe just the ground work does as the actual act is almost always instantaneous in comparison. Anyways, as I were saying, reproduction is difficult. If so then what word may quantify the degree of hardness it takes to reproduce someone exactly like the reproducer. And if that's dealt with, try doing the exact same thing alone, without anyone else involved. 
Sounds impossible? Maybe to living beings. Computer programs don't submit to hormones and are evenly potent in all matters, be it reproduction.
All programs do something, in most cases they produce something i.e. give something as output. In some cases, a computer program may need to produce it self e.g. viruses. Such programs are said to reproduce. This post is about a much strict version of such programs. This is about quines.

A quine is a non-empty computer program which takes no input and produces a copy of its own source code as its only output.

When a quine (or any reproducing program) is compiled, we get a binary which is capable of producing the source. Just consider how bizarre this is. Normally it is the source code that gives us the binary (also true in this case) but this time we also get a binary which gives us the source code.
The definition of a quine really takes away the easy ways to reproduce:
First of all one can say that since a nothing program will produce nothing, it is in a way reproducing an exact copy of itself i.e. nothing.
Secondly, one can always write a program that prints a string passed as an argument. In this case the programmer can just pass the entire source code of the program as argument to the running binary.
The real fun is in implementing a program that prints its source code exactly and does not take any arguments. This may sound impossible algorithmic-ally but with a little string formatting trickery things become rather easy. Here is one implemented by me, it is of course inspired by many such found around the web. I would invite you to try one yourself and then look up the solution. 
In hindsight, the program is kind of a cheat. It takes an argument from within, not from the user. But still arguments are involved.   

No comments:

Post a Comment