# An Interactive Quine in Clojure

Yehonathan Sharvit

11 Mar 2019

â€˘

• Clojure

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
; 42
``````
``````
[1 2 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.

I hope that now you are motivated to learn, step by step, how we this quine was discovered.

## Quine: first attempt

Like we wrote before, 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))))
``````

Thatâ€™s normal: self-referentiality is very often a dizzy activity. â€‹ â€‹ If you want to learn more about Clojure, start reading my Get Programming with Clojure book today! By the time you finish your final capstone project, youâ€™ll be designing Clojure functions, apps, and libraries like a pro!

Yehonathan Sharvit

Full-Stack Web Consultant. Expert in Clojure, ClojureScript and Javascript.

See other articles by Yehonathan

Related jobs

See all

### WorksHub

CareersCompaniesSitemapFunctional WorksBlockchain WorksJavaScript WorksAI WorksGolang WorksJava WorksPython WorksRemote Works

### Locations

hello@works-hub.com

Ground Floor, Verse Building, 18 Brunswick Place, London, N1 6DZ

108 E 16th Street, New York, NY 10003