Self-printing Algorithm

Source: Internet
Author: User
Self-Printing Algorithm In Program Running, do not read any additional information, only use the program Code To print out the program code itself.
A complete self-printing program contains at least two parts: The description and execution body of the information. The two are mutually expressed. In the description section, strings are used to store the code for execution. The execution body code prints the description string. Generally, in order to completely output the program code, it is necessary to splice the modifier content for the description string in the execution body, which is considered as part of the execution body. The code must be printed in two ways to print the description text into a new description text and a new execution body.
This process varies Programming Language You can use some tips to simplify it. For example, in the book "The Mystery of efficient programs", this C language self-printing program is demonstrated: Main ()
{
Printf (A, 34, A = "Main (a) {printf (A, 34, A = % C % S % C, 34);}", 34 );
}

This program cleverly utilizes the formatting and parsing of printf to write the program code concise and clear.
If the self-printing algorithm can only print its own code, it is just a toy. In fact, we can carry other code in self-printing as long as we patiently operate the string. We can see the following Python code: S= '''Def printself (input ):
Print "s =" + CHR (047) + CHR (047) + CHR (047) + input + CHR (047) + CHR (047) + CHR (047) + "\ n" + input + "\ n" + printself (s)'''

Def Printself (input ):
Print   " S = "   + CHR ( 047 ) + CHR ( 047 ) + CHR ( 047 ) + Input + CHR ( 047 ) + CHR ( 047 ) + CHR ( 047 ) +   " \ N "   + Input +   " \ N "   +   " Printself (s) "

Printself (s)

This is also a self-printing program, and its format is very loose, which makes it easy for us to add new code in it: S= '''F = open ("C:/txt.txt ")
Def printself (input ):
Print "s =" + CHR (047) + CHR (047) + CHR (047) + input + CHR (047) + CHR (047) + CHR (047) + "\ n" + input + "\ n" + print "Hello World" + printself (s)'''

F = Open ( " C:/txt.txt " )
Def Printself (input ):
Print   " S = "   + CHR ( 047 ) + CHR ( 047 ) + CHR ( 047 ) + Input + CHR ( 047 ) + CHR ( 047 ) + CHR ( 047 ) +   " \ N "   + Input +   " \ N "   +   " Printself (s) "

Printself (s)

After executing this program, we can open a file object during printing. If we follow this idea, we can also write the program code in the text; UseExecFunction can also be executed repeatedly by the program.

Of course, the character string operation capabilities and logical structure restrictions of different languages are different in the way of self-printing, and the flavor of the Code is also different. Contestants used multiple languages, includingC ++,Java,C #,PythonSorry, we didn't see anything famous for its character string operation capability.PerlLanguage works should also be unique. Most contestants correctly implement the self-printing algorithm, but the code length is quite different. In addition to the differences in the use of skills, there are also restrictions on the language itself.

Winner of this competition Shi fan Mr provided several language versions, of whichPythonThe well-deserved version is called the best work of this competition:

Print ( Lambda S: S. Replace (CHR ( 042 ), CHR ( 047 )) % S )( ' Print (lambda S: S. Replace (CHR (042), CHR (047) % s) ("% s ") ' )

This compact and exquisite Code fully embodies the subtlety of the Self-printing algorithm. PairLambdaThe application is also just right.PythonAuthorGuido van rosumPrepare to cancelLambdaIf he has the chance to read Shi fan Mr.'s code, I wonder if it will change his mind.

The self-printing algorithm is very interesting, and it embodies superb micro-skills. On the other hand, self-printing also has its metaphysical philosophical taste. It demonstrates in mathematics how to construct a system that can be self-copied and self-expressed. In fact, the description text in the Self-printing program corresponds to the chromosome of cell biology. The self-printing algorithm can be seen as a mathematical abstraction of cell life. As we can see above, other information can be carried in the Self-Printing Program, which indicates that this model can be used for functions other than "Reproduction. The self-printing algorithm subverts the theory of "God creates life. Scientists in the biochemical field have moved from urea, amino acids, and insulin to the Construction of cell life, the self-printing algorithm adds a mathematical road sign to this seemingly endless path of heaven.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.