## What’s a quine?

According to Wikipedia, a quine is a non-empty computer program which takes no input and produces a copy of its own source code as its only output.

In other words, a quine is a code snippet whose output is exactly the same as its source code. In the Clojure world, we would say that a quine is a code expression that evaluates to itself.

We say code expression in opposition with data expressions that trivially update to themselves:

``````42
``````
``````[1 2 42]
``````

## TL;DR

``````((fn [x] (list x (list 'quote x))) '(fn [x] (list x (list 'quote x))))
``````

Indeed, the output is exactly the same as the source code.

## Quine: first attempt

A quine is a piece of code that evaluates to itself. As a first attempt, we might be tempted to try to write a function that returns its source code as a form. After all, Clojure is homoiconic: the structure of the code is the same as the structure of the data.

Let’s try to write an anonymous function with no arguments that returns its code as a quoted form. This function does nothing so its code should be `(fn [])`. So let’s write and call an anonymous function that returns `'(fn [])`:

``````((fn [] '(fn [])))
``````

The problem is that now our anonymous function doesn’t do nothing. In fact, it returns `'(fn [])`. So the body of the function is now `'(fn [])`.

No problem, let’s improve our anonymous function so that it returns `'(fn [] '(fn []))`:

``````((fn [] '(fn [] '(fn []))))
``````

But now, the problem is that our anonynous function returns `'(fn [] '(fn [])`.

This strategy is not going to work: we will always be missing a `(fn [])` wrapping level.

## Quine: self referentiality

You might already have guessed that a real quine is going to involve self-referentiality.

Let’s find a function `f` and an argument `x` such that such that `(f x)` is exactly `(f x)`.

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! `(f 'f)` indeed evaluates to itself.

The only problem is that our code contains the definition of `f` but our output doesn’t.

In order to overcome this issue, we need to define `f` as an anonymous function.

As a first step, 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)
``````

Finally 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))))
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.88132334,"math_prob":0.99057484,"size":2564,"snap":"2021-31-2021-39","text_gpt3_token_len":679,"char_repetition_ratio":0.14257812,"word_repetition_ratio":0.05679513,"special_character_ratio":0.28042123,"punctuation_ratio":0.09090909,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9957804,"pos_list":[0,1,2],"im_url_duplicate_count":[null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-25T09:12:47Z\",\"WARC-Record-ID\":\"<urn:uuid:e08217ca-6a06-40a6-ad66-05b78347267b>\",\"Content-Length\":\"23083\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:188d8733-9cab-4d63-8da7-83a9d0f28a0d>\",\"WARC-Concurrent-To\":\"<urn:uuid:367ba1f2-d15a-4851-b683-512084bb91bc>\",\"WARC-IP-Address\":\"185.199.110.153\",\"WARC-Target-URI\":\"https://blog.klipse.tech/clojure/2019/01/08/quines-in-clojure.html\",\"WARC-Payload-Digest\":\"sha1:LVAI7BE4WBAOVYQVIRSZQBTKG4W6WWLO\",\"WARC-Block-Digest\":\"sha1:RROTDN2CJUMAUCWFY6IRENWEORFLARZQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046151641.83_warc_CC-MAIN-20210725080735-20210725110735-00573.warc.gz\"}"}