Constructing quines (self-reproducing functions)

前端 未结 5 1528
走了就别回头了
走了就别回头了 2021-02-19 03:47

Has anyone constructed a quine (\"A program that generates a copy of its own source text as its complete output\": http://www.nyx.net/~gthompso/quine.htm) in R? (The [quine] ta

5条回答
  •  一整个雨季
    2021-02-19 04:46

    Here is a real Quine, a program (not a function) that generates a copy of its own source text as its complete output.

    On console,

    # y1.R is a quine program
    $ cat y1.R
    (function(x){cat(x);cat('(');cat(intToUtf8(0x0022));cat(x);cat(intToUtf8(0x0022));cat(')')})("(function(x){cat(x);cat('(');cat(intToUtf8(0x0022));cat(x);cat(intToUtf8(0x0022));cat(')')})")
    
    # execute y1.R and show output
    $ /usr/bin/R --vanilla --slave < y1.R
    (function(x){cat(x);cat('(');cat(intToUtf8(0x0022));cat(x);cat(intToUtf8(0x0022));cat(')')})("(function(x){cat(x);cat('(');cat(intToUtf8(0x0022));cat(x);cat(intToUtf8(0x0022));cat(')')})")
    
    # save the output of the execution of y1
    $ /usr/bin/R --vanilla --slave < y1.R > y2.R
    
    # compare input and output -- exactly same.
    $ diff y1.R y2.R
    

    probably this is not the shortest one.

    UPDATED:

    and slightly shorter version:

    (function(x){cat(x,'(',d<-intToUtf8(0x0022),x,d,')',sep='')})("(function(x){cat(x,'(',d<-intToUtf8(0x0022),x,d,')',sep='')})")
    

提交回复
热议问题