查看“︁自產生程式”︁的源代码
←
自產生程式
跳转到导航
跳转到搜索
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{noteTA |G1=IT }} '''自產生程式'''({{lang-en|Quine}}),指的是輸出結果為[[计算机程序|程式]]自身[[源碼]]的程式。其英文名称以美國哲學家[[威拉德·冯·奥曼·蒯因|奎恩]]({{lang|en|Willard Van Orman Quine}})命名, 能夠直接讀取自己源碼、讀入使用者輸入或空白的程式一般都不視為自產生程式。 ==起源== 这种编程思想在计算机刚刚兴起的时候就已出现。Paul Bratley发表的文章《Computer Recreations: Self-Reproducing Automata》也对此进行了讨论。<ref name="Bratley_Millo">{{cite journal | last1 = Bratley | first1 = Paul | authorlink1 = Paul Bratley | last2 = Millo | first2 = Jean | title = Computer Recreations: Self-Reproducing Automata | journal = Software Practice and Experience | volume = 2 | year = 1972 | pages = 397–400 | doi=10.1002/spe.4380020411 }}</ref>而已知最早的這類程式在1960年代於[[愛丁堡大學]]出現,由Hamish Dewar以{{tsl|en|Atlas Autocode||Atlas Autocode}}編寫: %BEGIN !THIS IS A SELF-REPRODUCING PROGRAM %ROUTINESPEC R R PRINT SYMBOL(39) R PRINT SYMBOL(39) NEWLINE %CAPTION %END~ %CAPTION %ENDOFPROGRAM~ %ROUTINE R %PRINTTEXT ' %BEGIN !THIS IS A SELF-REPRODUCING PROGRAM %ROUTINESPEC R R PRINT SYMBOL(39) R PRINT SYMBOL(39) NEWLINE %CAPTION %END~ %CAPTION %ENDOFPROGRAM~ %ROUTINE R %PRINTTEXT ' %END %ENDOFPROGRAM ==原理== 我們先定義一個函數<math>q</math>,對於一個字串<math>w_a</math>,<math>q(w_a)</math>經過程式語言的解釋會變成<math>w_b</math>。 對於一個程式<math>P</math>而言,以下會使用<math>\langle P \rangle</math>來表示程式P的描述(即程式碼)。 建立一個程式'''SELF''',SELF由A、B所組成。換言之<math> \langle SELF \rangle=\langle A \rangle \langle B \rangle</math>。且會先執行A再執行B。 A=“儲存<math>\langle B \rangle</math>” B=“對於輸入<M> ,而M為一段程式碼。一、計算出q(<M>) 二、把計算結果和<M>結合起來 三、印出所出求出描述。” 而自生實際執行的過程為: :一、首先A先執行,會得到<math> \langle B \rangle </math>。 :二、B開始執行,然後被輸入<math>\langle B \rangle</math>(即<math>M=B</math>)。 :三、B利用<math> \langle B \rangle </math> ,可以計算出<math> q(\langle B \rangle)</math>,並以此計算出<math> \langle A \rangle</math>。將<math> \langle A \rangle </math>與<math> \langle B \rangle </math>組合成一個新的程式的描述<math> \langle SELF \rangle </math>。 :四、輸出<math>\langle SELF \rangle</math>。 ==示例== 在Quine的定义里,程序不能有任何形式的输入,否则将被视为“作弊”。例如,一个程序读取该程序自身的源代码然后打印出来,利用这种方法的程序属于作弊的Quine。 ===Perl=== 一个用[[Perl]]编写的无作弊的Quine: <syntaxhighlight lang="Perl"> $_=q{print"\$_=q{$_};eval"};eval </syntaxhighlight> ===Python=== [[Python]]本身提供<code>repr()</code>運算,其作用大致等同於上述之<code>q()</code>。如: <syntaxhighlight lang="python3" line="1"> # repr() function >>> w='Hello World\nHwllo World' >>> print (w) Hello World Hwllo World >>> repr(w) "'Hello World\\nHwllo World'" # Quine (line 11, 13, 14) A: >>> x='y="x="+repr(x)+"\\n"\nprint (y+x)' B: >>> y="x="+repr(x)+"\n" >>> print (y+x) x='y="x="+repr(x)+"\\n"\nprint (y+x)' y="x="+repr(x)+"\n" print (y+x) </syntaxhighlight> ==參見== * [[塔珀自指公式]] ==參考文獻== {{Reflist}} ==外部連結== * [http://www.nyx.net/~gthompso/quine.htm The Quine Page] {{Wayback|url=http://www.nyx.net/~gthompso/quine.htm |date=20210507114041 }} * [http://en.wikisource.org/wiki/Quines wikisource:Quines]{{dead link|date=2018年4月 |bot=InternetArchiveBot |fix-attempted=yes }} * [http://www.javaworld.com.tw/jute/post/view?bid=35&id=54909 JavaWorld@TW - 自產生程式] {{Wayback|url=http://www.javaworld.com.tw/jute/post/view?bid=35&id=54909 |date=20160309222336 }} {{标准测试项目}} [[Category:计算机编程|Zichanshengchengxu]]
该页面使用的模板:
Template:Cite journal
(
查看源代码
)
Template:Dead link
(
查看源代码
)
Template:Lang
(
查看源代码
)
Template:Lang-en
(
查看源代码
)
Template:NoteTA
(
查看源代码
)
Template:Reflist
(
查看源代码
)
Template:Tsl
(
查看源代码
)
Template:Wayback
(
查看源代码
)
Template:标准测试项目
(
查看源代码
)
返回
自產生程式
。
导航菜单
个人工具
登录
命名空间
页面
讨论
不转换
查看
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
特殊页面
工具
链入页面
相关更改
页面信息