Constructing quines (self-reproducing functions)

前端 未结 5 1522
走了就别回头了
走了就别回头了 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:32

    Using what body does as inspiration, call can be used to reproduce the calling command:

    f <- function () 
    {
        call("<-", as.name(as.character(sys.calls()[[1]])), sys.function(sys.parent()))
    }
    

    Which outputs:

    > f()
    f <- function () 
    {
        call("<-", as.name(as.character(sys.calls()[[1]])), sys.function(sys.parent()))
    }
    

提交回复
热议问题