Quine programs output their own programming instructions as text. It is equivalent to describe such programs as self-reproducing. The name is taken from a Philosopher W.V. Quine, who studied self-reference. This document concerns quines written by Geoff Swift, please give credit if you copy the code.
Writing a quine is quite difficult in most programming languages. Although this serves no real purpose, the technique of source code generation can be useful in creating large programs. The challenge is however to write quines which are as short as possible.
JavaScript
This JavaScript quine was written several years ago, and has been well received on the web. This code has been ported to another language, and used in educational materials. See my egosurfing links for details.
a=new Array();a[0]='a=new Array();';a[1]='[';a[2]=']';a[3]='\'';a[4]='\\';a[5]='=';a[6]='a';a[7]=';';a[8]='';a[9]='for(i=0;i<10;i++)document.write((i==0?a[0]:a[8])+a[6]+a[1]+i+a[2]+a[5]+a[3]+((i==3||i==4)?a[4]:a[8])+a[i]+a[3]+a[7]+(i==9?a[9]:a[8]))';for(i=0;i<10;i++)document.write((i==0?a[0]:a[8])+a[6]+a[1]+i+a[2]+a[5]+a[3]+((i==3||i==4)?a[4]:a[8])+a[i]+a[3]+a[7]+(i==9?a[9]:a[8]))The array elements hold the small segments of the text, and the for loop prints out the declaration of each array element in sequence. This is fairly generic, but there are some special cases handled using the tertiary "immediate if" operator.
Pascal
I wrote a Pascal quine to use in my email .signature file. My aim was a quine that would fit on a standard width 80 character display, and comply with the RFC 1855 Netiquette Guideline. See my email signature below:
--
Geoffrey Swift
const a='const a=';b='begin write(a,#39,a,#39#59#98#61#39,b,#39#59#10,b) end.';
begin write(a,#39,a,#39#59#98#61#39,b,#39#59#10,b) end.
Wikisource quotes my signature code as their first Pascal quine, and a translation of the article features in various international versions of Wikipedia.
Using the ^ operator for character literals,
instead of using #, it is possible to reduce the
quine further. This works fine when the
ASCII value is more than two decimal digits, but some characters
can't be represented this way.
The ^ operator, is case sensitive.
Both ^g and
^G denote an
ASCII BEL / bell, so the single quote character remains as
#39.
With further tweaking this reduces to a a rather illegible, but quite respectable 100 characters:
const a=';begin write(^#^/^.^3^4^`^!^}#39,a,#39,a)end.';begin write(^#^/^.^3^4^`^!^}#39,a,#39,a)end.
This was tested with Turbo Pascal, Free Pascal and Delphi.
Delphi compiles these best with command line dcc32 -CC, since
there is no explicit {$APPTYPE CONSOLE} declaration.
SQL
Having worked extensively with SQL in my employment, I knew a SQL quine could be done. I wrote this quine using Microsoft SQL Server.
set quoted_identifier off
declare @a varchar(147)
declare @b varchar(37)
declare @sq char
declare @dq char
declare @qm char
set @a='set quoted_identifier off
declare @a varchar(147)
declare @b varchar(37)
declare @sq char
declare @dq char
declare @qm char
set @a='
set @sq="'"
set @dq='"'
set @qm='?'
set @b='
print replace(@a,@qm,@sq)+@a+@sq+@b'
print @a+@sq+@a+@sq
print 'set @sq='+@dq+@sq+@dq
print 'set @dq='+@sq+@dq+@sq
print 'set @qm='+@sq+@qm+@sq
print 'set @b='+@sq+@b+@sq
set @a='print @a+@sq+@a+@sq
print ?set @sq=?+@dq+@sq+@dq
print ?set @dq=?+@sq+@dq+@sq
print ?set @qm=?+@sq+@qm+@sq
print ?set @b=?+@sq+@b+@sq
set @a=?'
print replace(@a,@qm,@sq)+@a+@sq+@b