[R] Some Programming Humor - Quine

Wolfgang Koller koller at isis.wu-wien.ac.at
Thu May 15 14:03:17 CEST 2003


Hi Barry! Hi helplist!

What about this piece of code?

quc1 <- "function () { STR <- "
quc2 <-
"substr(deparse(paste)[1],start=24,stop=24); "
quc3 <- "recstr <-
function(str=character(1),pos=195) "
quc4 <-
"paste(substr(str,1,pos-1),"
quc5 <- "str,substr(str,pos,1000),sep=STR);
"
quc6 <- "eval(parse(text=recstr("
qucode <-
paste(quc1,quc2,quc3,quc4,quc5,quc6,sep="")
qucode <-
paste(qucode,paste(qucode,")))}",sep=""),")))}",sep="\"")
quine <-
eval(parse(text=qucode))

It passes the following quine-test:

> all(deparse(quine)==deparse(quine()))
[1] TRUE
> all(deparse(quine())==deparse(quine()()))
[1] TRUE

By the way, some questions that arose in this programming exercise:
a) is there a simpler way to do the assignment 
     STR <- "\""
   without using quotes?

b) what does this cryptic <environment: 034768AC> (or similar) mean that
does appear after the return value of quine() but not after the result of
quine?

c) is there a better way to check if two functions are identical than the
way suggested above?


Wolfgang


-------------------------------------------------
Wolfgang Koller,  wolfgang.koller at wu-wien.ac.at
Forschungsinstitut für Europafragen
Wirtschaftsuniversität Wien
Althanstraße 39-45, 1090 Vienna, Austria
Tel: ++43/1/31336/4147  Fax: ++43/1/31336/758
http://fgr.wu-wien.ac.at/institut/ef/ief-home.htm




More information about the R-help mailing list