Quine (dc)

From LiteratePrograms

Jump to: navigation, search
Other implementations: BASIC | Clojure | dc | Erlang | Forth | JavaScript / URI | Lisp | Oz | Perl | Python | Smalltalk

an initial attempt

We start in classic form with

[91PP6120568P]dx

or, from the command line,

dc -e "[[dc -e ]P8795PP93P[dx]P8714P]dx"

[ ] quotes
d duplicates
x executes

→ the ending dx produces the classic quine pattern of code preceded by its quotation.

P prints an integer in base-256/ascii

→ quotation characters can be avoided by entering strings as base-10 numbers

For example, 6120568 == 0x5D6478 == "]dx", and so by punning between the numeric and string representations of a value, we reach...

the final result

<<quine.dc>>=
6581840dnP

This quine duplicates 6581840, printing it once as the number 6581840, and once as the string "dnP".


Most invocations of P in the quotation-using quines occur immediately after their arguments. Can you spot the P that is used to print the entire outer quotation?

historical note

These days, using a literal both for its numeric value and its executable potential is unlikely to be found, outside of a quine. (inside of a quine, it's too dark to code) But back in the days of bit-bumming, when short registers and deep latencies made programmers miserly, Mel, a Real Programmer, and his peers were fond of them:

      "You never know where it's going to put things",
      he explained, "so you'd have to use separate constants".
      It was a long time before I understood that remark.
      Since Mel knew the numerical value
      of every operation code,
      and assigned his own drum addresses,
      every instruction he wrote could also be considered
      a numerical constant.
Download code
Views