This challenge is an extension of 'Golf a mutual quine'.

Using three languages of your choice, create a third order Ouroboros program.

That is, in language A, write a program pA which outputs program pB in language B. Program pB should output program pC in language C, which in turn outputs the original program pA in language A.

No two languages in A, B, and C can be the same or subsets or supersets of each other. None of pA, pB or pC may be identical.

For example, a Haskell program outputs a Perl program which outputs a java program which outputs the original Haskell program would be valid.

On the other hand, a C# 4 program which outputs a C# 3 program which outputs a C# 2 program is invalid. Even a Perl -> Ruby -> Bash combination would be invalid if, say, the Perl program and the Ruby program were identical.

This is code golf, so the shortest program pA wins.


3 Answers 3


Python -> Perl -> Ruby, 48 characters

Adaption of my previous answer. Running

s='print q<puts %%q{s=%r;print s%%s}>';print s%s

with Python generates this Perl snippet

print q<puts %q{s='print q<puts %%q{s=%r;print s%%s}>';print s%s}>

which generates the following Ruby code

puts %q{s='print q<puts %%q{s=%r;print s%%s}>';print s%s}

which then prints the original Python snippet:

diff -s <(ruby <(perl <(python thirdorderquine.py))) thirdorderquine.py 
Files /dev/fd/63 and thirdorderquine.py are identical

Perl -> PHP -> HTML + JavaScript, 105 chars

I wanted to make the chain of languages somehow meaningful, so I figured I'd write a PHP script that generates a HTML page containing JavaScript. For the third language I chose Perl, just because I like Perl. :)

Some might consider this four languages, but I don't really count HTML as separate from JavaScript here — it's a markup language, not a programming language. Anyway, here are the three versions:

Perl (105 chars):


PHP (165 chars):


HTML + JavaScript (235 chars):


(Ps. Yes, I know I could've made the PHP step an almost-noop, e.g. just generating HTML + JS code in Perl and appending <?php to it, but that felt too much like cheating. In this solution, the code is actually processed in PHP instead of just being copied verbatim.)


Underload → sed → Perl, 23 bytes

Can probably get this down lower with better choices of languages. Arguably noncompeting because the rule "sed programs can take an empty line as argument" postdates the contest.

The Underload program:


generates the sed program:


which generates the Perl program:


(note: there are two trailing newlines here), which generates the original Underload program again:


The main aim here is to find languages in which strings are nestable (i.e. you can just quote a string by enclosing it in delimiters, rather than having to escape it); Underload has (), Perl has q(), and in sed a string continues until whitespace (which is implicitly nestable if there's no whitespace in the program). There's probably an esolang or golfing language out there with a "print the rest of the current line, not followed by newlines" instruction, which would be ideal here, but I haven't spent all that much time looking for one; you could save 8 bytes minus the length of the instruction in that case. (Jelly almost works but its instruction doesn't quote itself. Besides, it postdates the challenge.)

You can reduce this to 22 bytes like this:


(with one trailing newline, like a regular text file, rather than the zero you normally get in golf). However, this requires an Underload interpreter that's OK with treating newline as a no-op. Try It Online!'s does, but I think it postdates the challenge.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.