Clojure
Let’s find a unary function f
and an argument x
such that such that (f x)
is exactly (f x)
.
It’s not hard to guess that this is going to involve self-referentiality. Therefore a good guess for x
is 'f
.
So let’s write our function f
such that (f 'f)
is (f 'f)
:
(defn f [x] '(f 'f))
(f 'f)
It works!
Now, we need to anonymize f
.
So let’s rewite f
in such a way that it will return (f 'f)
not for all the arguments but only when we pass 'f
to f
:
(defn f [x] (list x (list 'quote x)))
(f 'f)
Now if we replace f
by its definition, we get our quine:
((fn [x] (list x (list 'quote x))) '(fn [x] (list x (list 'quote x))))
Javascript
(function quine(){
console.log('(' + quine.toString() + ')();');
})();
HTML/CSS
Ruby
x = "x = %p; puts x %% x"; puts x % x
Scheme
((lambda (x)
(list x (list (quote quote) x)))
(quote
(lambda (x)
(list x (list (quote quote) x)))))
Python
Python’s %r
format conversion uses the repr()
function to return a string containing the source code representation of its argument:
x = 'x = %r\nprint(x %% x)'
print(x % x)
With the new str.format
:
x = 'x = {!r};print(x.format(x))';print(x.format(x))