Skip to content

Instantly share code, notes, and snippets.

@Lull3rSkat3r
Created November 12, 2012 20:01
Show Gist options
  • Save Lull3rSkat3r/4061542 to your computer and use it in GitHub Desktop.
Save Lull3rSkat3r/4061542 to your computer and use it in GitHub Desktop.
This a a javascript program that uses the concept of self description when adding two numbers.
/* Author: Corey Stubbs cas5542 (AT) gmail [DOT] com
* Date: 11/12/2012
* About: This a a javascript program that uses the concept of self description when adding two numbers.
* The main purpose of this program is found within the addTwo function of Program. The function
* will add the two numbers, a and b, iff the third parameter, string, is the string encoding of Program.
* If string is not the encoding, the function will always return 0. Very strange, I know, but this is a
* very important concept in computer science and is made possible via the recursion theorem. This
* particular kind of program is know as a quine. I have provided two links in regards to recursion
* theorem and quines. If you have any questions let me know!
*
* Recursion Theorem: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-045j-automata-computability-and-complexity-spring-2011/lecture-notes/MIT6_045JS11_lec10.pdf
* Quine Link: http://en.wikipedia.org/wiki/Quine_(computing)
*
* To Run: If you have nodejs installed, simply run 'node Quine.js'. If not, copy this file into the developer
* tools console of chrome and go from there.
*/
/****************************
* Begin Quine Program *
*****************************/
function Program(){
var self = this;
var me = [
'function Program(){\n',
'var self = this;\n',
'\tvar me = [\n',
'\t]\n',
'\t\n',
'\tself.PrintSelf = function(){\n',
'\t\tvar returnVal = "";\n',
'\t\tfor(var i = 0; i < 3; i++){\n\t\t\treturnVal += me[i];\n\t\t}\n',
'\t\tfor(var i = 0; i < 20; i++){\n\t\t\treturnVal += "\'" + me[i].replace(/\t/g,\'\\t\').replace(/\n/g,\'\\n\') +"\',";\n\t\t}\n',
'\t\tfor(var i = 3; i < 20; i++){\n\t\t\treturnVal += me[i];\n\t\t}\n',
'\t\treturn returnVal;\n',
'\t}\n',
'\tself.addTwo = function(a,b, string){\n',
'\t\tif(string !== self.PrintSelf()){\n',
'\t\t\treturn a+b;\n',
'\t\t} else{\n',
'\t\t\treturn 0;\n',
'\t\t}\n',
'\t}\n',
'};\n',
]
self.PrintSelf = function(){
var returnVal = "";
for(var i = 0; i < 3; i++){
returnVal += me[i];
}
for(var i = 0; i < 20; i++){
returnVal += "\t'" + me[i].replace(/\t/g,'\\t').replace(/\n/g,'\\n') +"',\n";
}
for(var i = 3; i < 20; i++){
returnVal += me[i];
}
return returnVal;
}
self.addTwo = function(a,b, string){
if(string !== self.PrintSelf()){
return a+b;
} else{
return 0;
}
}
};
/****************************
* End Quine Program *
*****************************/
/****************************
* Begin Test Program *
*****************************/
var testPogram = new Program();
console.log(testPogram.PrintSelf());
console.log(testPogram.addTwo(1,2,"test"));
console.log(testPogram.addTwo(1,2,testPogram.PrintSelf()));
/****************************
* End Test Program *
*****************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment