Search code examples
programming-languageslanguage-designquine

Language features helpful for writing quines (self-printing programs)?


OK, for those who have never encountered the term, a quine is a "self-replicating" computer program. To be more specific, one which - upon execution - produces a copy of its own source code as its only output.

The quines can, of course, be developed in many programming languages (but not all); but some languages are obviously more suited to producing quines than others (to clearly understand the somewhat subjective-sounding "more suited", look at a Haskell example vs. C example in the Wiki page - and I provide my more-objective definition below).

The question I have is, from programming language perspective, what language features (either theoretical design ones or syntax sugar) make the language more suitable/helpful for writing quines?

My definition of "more suitable" is "quines are easier to write" and "are shorter/more readable/less obfuscated". But you're welcome to add more criteria that are at least somewhat objective.

Please note that this question explicitly excludes degenerate cases, like a language which is designed to contain "print_a_quine" primitive.


Solution

  • I am not entirely sure, so correct me if anyone of you knows better. I agree with both other answers, going further by explaining, that a quine is this:

    Y g

    where Y is a Y fixed-point combinator (or any other fixed-point combinator), which means in lambda calculus:

    Y g = g(Y g)

    now, it is quite apparent, that we need the code to be data and g be a function which will print its arguments.

    So to summarize we need for constructing such a quines functions, printing function, fixed-point combinator and call-by-name evaluation strategy.

    The smallest language that satisfies this conditions is AFAIK Zot from the Iota and Jot family.