Skip to content

tingstad/quine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Quine

A “quine” is a self-replicating program — its output is a copy of its own source code.

Here is an example I wrote in Python:

c = """
c = {1}{0}{1}
print(c.format(c, '"'*3))
"""
print(c.format(c, '"'*3))

Shell quine

Shell script quines without "%s"

This shell script prints an exact copy of itself. This means that cat quine.sh and ./quine.sh are equivalent:

quine/quine.sh

Lines 1 to 23 in f1a460d

#!/bin/sh
# https://github.com/tingstad/quine
b='\'
s=\'
top='#!/bin/sh
# https://github.com/tingstad/quine
'
src='
echo "$top
b=$s$b$s
s=$b$s
top=$s$top$s
src=$s$src$s
$src"
'
echo "$top
b=$s$b$s
s=$b$s
top=$s$top$s
src=$s$src$s
$src"

It's therefore also equivalent to running e.g. sh <(sh <(./quine.sh)).

I did not want to use printf %s for this quine. The trick was to get the quoting right on the "recursion line", #14 (which has to match #9), I found the solution here.

In quine2.sh I wanted to avoid variable assignments and minimize string interpretation (no % and $) and just use simple printf and zero-argument functions/procedures.

PDF quine

I was curious if it was possible — it took many hours and a few hundred lines, but I present:

📃 quine.pdf

(Notes: pdf.md)

Lempel-Ziv

Yet another LZ77/LZ1 (ZIP Deflate) quine

Using the rules

  • print M: prints the following M lines of input. Any command that is printed will not be run as an action.
  • repeat M N: repeats the last M lines of output, starting N lines from the end.

M is a whole number (e.g. 0, 1, 2, ...) N is a natural number (e.g. 1, 2, 3, ...)

my solution was:

print 1
print 1
print 1
print 1
print 1
print 1
repeat 3 2
print 2
repeat 3 2
print 2
repeat 3 2

If repeating before input is allowed, I had:

print 0
repeat 5 4
print 0
print 5
 print 0
 repeat 5 4
 print 0
 print 5
 print 0
repeat 5 4

Check out Zip Files All The Way Down by Russ Cox!

About

Self-replicating program

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages