We use cookies and other tracking technologies to improve your browsing experience on our site, analyze site traffic, and understand where our audience is coming from. To find out more, please read our privacy policy.

By choosing 'I Accept', you consent to our use of cookies and other tracking technologies.

We use cookies and other tracking technologies to improve your browsing experience on our site, analyze site traffic, and understand where our audience is coming from. To find out more, please read our privacy policy.

By choosing 'I Accept', you consent to our use of cookies and other tracking technologies. Less

We use cookies and other tracking technologies... More

Login or register
to apply for this job!

Login or register to start contributing with an article!

Login or register
to see more jobs from this company!

Login or register
to boost this post!

Show some love to the author of this blog by giving their post some rocket fuel 🚀.

Login or register to search for your ideal job!

Login or register to start working on this issue!

Engineers who find a new job through Functional Works average a 15% increase in salary 🚀

Blog hero image

An Interactive Quine in Clojure

Yehonathan Sharvit 10 January, 2019 (2 min read)

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.

Quine Blog.png

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

If you have no patience to read this article, here is the simplest Clojure Quine:

((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))))

Do you feel a headache?

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!

Originally published on blog.klipse.tech