20 comments

[ 2.9 ms ] story [ 64.0 ms ] thread
Interesting article. I'd never heard of this concept before and thought "That sounds trivial" until I started to read the article. I must admit though, I gave up halfway through when the C code got too esoteric for me to follow. But still feel I've learned something new today.

The article title seems a bit off though. When I read 'Self-Replicating Programs' [sic] it I immediately thought "Worms" but the article actually refers to programmes which can print out their own source code listing. Which seems to me to be a different challenge.

> When I read 'Self-Replicating Programs' [sic] it I immediately thought "Worms"

You are not wrong at all. At its base, any virus is a quine. Well, both computer viruses and biological viruses are quines. Life itself would not be possible without quines.

Source code inspection (or cp) is considered cheating for quines, normal viruses don't follow this rule
```

#!/bin/bash

cat $0

```

Or as was posted to https://news.ycombinator.com/item?id=29874052 by @jcalvinowens a few hours ago, we can also simplify this to just:

  #!/bin/cat
Example:

  $ echo '#!/bin/cat' > foo
  $ chmod +x foo
  $ cat foo
  #!/bin/cat
  $ ./foo 
  #!/bin/cat
or just an empty string for various languages
Seems like that would only work for languages where the last returned value is printed as-is when running it. Other languages wouldn't print `""` but have no output instead.
I think they meant that the program would be 0 bytes long, not two characters which are both quotation marks
Ah, I see. Funny lateral thinking exercise I guess, not so interesting in terms of actually creating a Quine :)
Wouldn't this be considered cheating according to the submission?

> The easiest way to do that, of course, is to seek the source file on the disk, open it, and print its contents. That may be done, but it is considered cheating; besides, the program might not know where the source file is, it may have access to only the compiled data, or the programming language may simply forbid that sort of operations.

Anyone interested in this should really read Hoffstadter's Gödel, Escher, Bach.
The most amazing multi-quine I've seen is the quine relay here: https://github.com/mame/quine-relay

It goes through 128 programming languages to regen its starting point.

Fun side fact: that's a project from Yusuke Endoh, who also happens to be the most prolific winner of the the obfuscated C contest (ioccc.org).
This is the first project I've ever seen with an excuse to declare as many dependencies as it does.
An interesting "application" are zip quines, where a compressed archive decompresses to itself. https://research.swtch.com/zip

I recently built a zip quine that was simultaneously a valid PDF to show off, and the most fun part was making the CRC32 check succeed. If the checksum had been based on a cryptographic hash function, it would've been essentially impossible, but fortunately CRC32 can be reversed simply by solving a system of linear equations.

Joy lang has a particularly beautiful quine:

    [[dup cons] dup cons]
You run it with the i combinator:

    [[dup cons] dup cons] i
     [dup cons] dup cons
     [dup cons] [dup cons] cons
    [[dup cons]  dup cons]