[exercise] Quines

0 views
Skip to first unread message

Matthew Fernandez

unread,
Sep 29, 2011, 5:11:46 AM9/29/11
to code-p...@googlegroups.com
Sorry we missed this month's exercise. I'm deep in the middle of some
assignment hell. To make up for it, I'm giving you something extra
flexible and (hopefully) extra interesting. Your task is to write a
quine (https://secure.wikimedia.org/wikipedia/en/wiki/Quine_%28computing%29).
It can perform any function you like (including do nothing except be a
quine).

The nice thing about this is that you can make this task as simple or
as complex as you like (for example, the empty string is a perfectly
valid quine) and it's also interesting from a mathematical point of
view, for those of us who like fix points. As usual, completely
optional and partial solutions or follow up questions are encouraged.

For next month's tools (in two days), I have some potential topics
that I wanted some votes for or against:
- Pre-processor hacks (I've picked up quite a repertoire of handy
ones in the last month)
- Cscope
- GNU Screen or Tmux

Sorry they're all quite low-level and C-ish, but it's what I'm doing
at the moment. If anyone has another topic they'd like to talk about
(or something they'd like me to talk about) please let me know.
Failing that, you'll get topic number 1, which I'm guessing will be of
no interest to many people, so it's in your best interests to vote :)

Tom Allen

unread,
Oct 4, 2011, 1:02:40 AM10/4/11
to code-p...@googlegroups.com
Python:
print open(__file__).read(),

Save this as a file called 'something.py' and run 'python something.py'. The comma is important, as print() will otherwise append a newline to the output, which would invalidate the quine.

Cheers,
Tom

Patrick Coleman

unread,
Oct 4, 2011, 9:28:03 PM10/4/11
to code-p...@googlegroups.com
The best quine I know is the empty file :)
other than that, one nice alternative is quine cycles, that generate N different programs in order;

Matthew Fernandez

unread,
Oct 4, 2011, 10:58:09 PM10/4/11
to code-p...@googlegroups.com
I saw that guy demo the quine cycle at RubyKaigi and it was
mind-blowing. Watching the world rotate, while seeing the code that's
actually doing it. Needless to say, didn't get much else done that
day.

Tom, nice work coping with the extraneous line endings as well!

Matthew Fernandez

unread,
Oct 16, 2011, 11:45:29 PM10/16/11
to code-p...@googlegroups.com
I was trying to come up with a quine that used a stack smash or
self-reference to print its binary form (so the result of "myprogram"
would be the same as "cat myprogram"). Something analogous to
puts((char*)&main), but I haven't quite got a good platform
independent solution yet. Any ideas? Maybe Python's inspect library
(http://docs.python.org/library/inspect.html) could yield something
interesting too.

Meanwhile, this month's exercise was inspired by Ken Thompson's
Reflections on Trusting Trust
(http://cm.bell-labs.com/who/ken/trust.html). If you haven't seen this
before, I highly recommend it. Be warned: it may make you doubt
everything you are sure about in computing.

Reply all
Reply to author
Forward
0 new messages