A Self-Reproducing Program
February 20, 2009
((lambda (x) `((lambda (x) ,x) ',x))
'`((lambda (x) ,x) ',x))
This is a problem with deep philosophical connotations, as described by Douglas Hofstadter in his Pulitzer-prize winning book Gödel, Escher, Bach: An Eternal Golden Braid. Ken Thompson exploits self-reproducing programs in his Turing Award lecture Reflections on Trusting Trust. Gary P. Thompson collects self-reproducing programs at The Quine Page. You can see the program at http://programmingpraxis.codepad.org/gVae4PRG.
I can’t believe I’d never actually tried to write a quine before. Here’s what I came up with:
http://pastebin.com/f1c8575a1
I think my C background is showing, as this may not be particularly Schemish.
I really wanted the output to be formatted like the original program, though. That proved more difficult than I expected. Perhaps there’s an easier way that I missed.
http://pastebin.com/f3706d30d
I used fold to create a q&d add-between function when translating it from PLT Scheme to R6RS. I’m not sure it’s a good implementation or not.
I do believe g wins the Internet.
print open(__file__).read(),
In perl I did:
system(“type $0”);
It can’t be that easy, what am I missing??
golang
In Python:
https://github.com/ftt/programming-praxis/blob/master/20090220-a-self-reproducing-program/program.py
# Slate’s answer In Ruby
puts File.read(__FILE__)
In old timey 8-bit BASIC:
And a real, non-cheater one in Common Lisp