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] tag pulls up lots of examples in Python, Java, ... but apparently none in R.)
f <- function() { body() }
comes close:
> f()
{
body()
}
but lacks the name of the function.
How about the shortest possibility? Most obfuscated?
edit: from the variety of answers below, it seems that there are a variety of ways to define self-referentiality and the environment in which it must occur:
- within the R environment: function
->
function (@bill_080) - within the OS/shell environment: program
->
program [more or less equivalent to program->
text]: (@kohske) - other: function
->
text (@JoshUlrich, @James, problem as defined above)
Notes:
- The thread from R-help pointed out by @Spacedman (which seems to emphasize obfuscation over brevity) suggests that
identical(quine,quine())
is a good test case, although it's tricky because environments get carried along:identical(quine,quine(),ignore.environment=TRUE)
might be easier. - A recent (Oct 2015) blog post provides another answer ...
This is the shortest I can come up with:
If you want a function that returns a function.....maybe this?
The output is:
While I'm not sure if this "counts" from a quine perspective (I stumbled across this question while trying to verify if it does), the script
will output
function(){}
. This works on the same principle as Joshua Ulrich's answer, just pared down to the essentials.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,
probably this is not the shortest one.
UPDATED:
and slightly shorter version:
Using what
body
does as inspiration,call
can be used to reproduce the calling command:Which outputs: