Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Writing the Most Boring Quine (rtpg.co)
63 points by todsacerdoti on March 4, 2023 | hide | past | favorite | 24 comments


I wouldn't say this is the most boring. In Python it is certainly an empty file.

It was interesting to see the author work through the major obstacles one by one when writing a quine. Glad they didn't take the actual boring route!


Yes, and if it's about the most minimal non-empty quine pretty sure it would be this:

    s='s=%r;print(s%%s)';print(s%s)
Anyway, let's make it a bit less boring and make it a C/Python polyglot (not relay) quine:

    #include<stdio.h>
    #define len int main(){char*
    #define zip return 0;}
    #if 0
    def printf(f,*a):print(f%a,end=str())
    #endif
    len
    s="#include<stdio.h>%c#define len int main(){char*%c#define zip return 0;}%c#if 0%cdef printf(f,*a):print(f%%a,end=str())%c#endif%clen%cs=%c%s%c;printf(s,10,10,10,10,10,10,10,34,s,34,10);zip%c";printf(s,10,10,10,10,10,10,10,34,s,34,10);zip
Same in C/Ruby:

    #include<stdio.h>
    #define puts(c)void main(){c;putchar(10);}
    #define dup char*
    dup
    s="#include<stdio.h>%c#define puts(c)void main(){c;putchar(10);}%c#define dup char*%cdup%cs=%c%s%c;puts(printf(s,10,10,10,10,34,s,34))";puts(printf(s,10,10,10,10,34,s,34))
Wonder if one can make that shorter.


Yeah. I wish the article talked about "the most boring nonempty quine".


Probably anyone reading this thread knows about this already :-) but that's like the https://en.wikipedia.org/wiki/Interesting_number_paradox


Maybe the empty quine is not boring by virtue of the fact it's not like all of the other quines.


2 is the odd prime for the same reason.


a non-empty quine in python would be `"hello world"`


Just writing a string in a python doesn’t output that string to stdout


yes it does


     $ ~ echo '"Hello world"' > quine.py
     $ ~ cat quine.py
     "Hello world"
     $ ~ python quine.py
     $ ~


Indeed. In C it is also the empty file, though it takes some compiler settings boogaloo to make it work.

See: https://www.ioccc.org/1994/smr.hint


There is one interesting conclusion to be derived from this exercise - in Python, '%r' is a fixed point of the function f(x)=x%x.

And yes, this is the most boring quine, you've punted the problem to the formatting mini-language and solved it in a manner equivalent of print(open(argv[0]).read()). I would say perhaps the ideal formulation for producing an interesting quine that doesn't leverage other languages would be "write a quine in C, using no preprocessor directives and no external functions except the POSIX write function, you may assume stdout is 1". So your program would look like:

    extern long write(int, const void *, unsigned long);
    int main(int, char**) {
        /* your code here, using write(1, buf, count) for output */
        return 0;
    }
You would of course end up writing a function equivalent to 'x%x' in Python, but getting there is a bit more exciting.


Or write a binary quine that doesn't read it's own program memory. I wrote a Python script that generates appropriate NASM code: https://github.com/panzi/binary-quine

Did it that way so I don't need to learn about the ELF format, but use several passes to get a binary that is a quine.


No reason to stop at print("print()"), you can nest further, print('print("print()")'). And then you can short-circuit the process, write a single program which first does print(), then print("print()"), then print('print("print()")'), then the next level, then the next level, etc. And no need to stop there: once you have that program, you can print IT. And then continue the process.

I've done this up to level omega^omega at my Library of Intuitive Ordinal Notations: https://github.com/semitrivial/ions/


That’s also the idea of https://codegolf.stackexchange.com/questions/48931/make-the-..., where people have taken it up to much larger ordinals.


Or write an infinitely long source code. This is technically---and I stress only technically---possible in not all but many languages, where an infinitely sized code is not explicitly disallowed. C is a surprisingly rare counterexample, which even doesn't allow for an infinitely sized memory anyway (because the pointer should be finitely sized).


A slightly more general formula works in any language that can manipulate strings, without leveraging the built-in escaping mechanism. Basically, create a program that prints a variable, but replaces a token in the variable with the contents of another variable, properly escaped for the target language. Finally, replace the content of the variable with the entire source code of the program. Naively applying this formula to Python gives you this: step 1:

    CODE = 'XXX'
    print(CODE.replace('"XXX"', repr(CODE), 1))
Step 2:

    CODE = 'CODE = "XXX"\nprint(CODE.replace(\'"XXX"\', repr(CODE), 1))'
    print(CODE.replace('"XXX"', repr(CODE), 1))
The beauty of this approach is that you could include the definition of the repr function in the code, so it works in any language that can manipulate strings. Another generally-applicable approach is to use eval: write a program that prints a variable assignment and an eval of that variable, then wrap that program in a variable and eval it:

    code = 'print("code = {!r}\\neval(code)".format(code))'
    eval(code)
Again this uses "!r" for brevity but it could easily include the function responsible for doing the escaping. Requires eval, so not as universally applicable as the previous method (but produces shorter quines).

Anyone aware of other generally-applicable techniques for producing quines?


My fav:

    #!/usr/bin/env cat
Or even shorter:

    #!/bin/cat


Can't you just use an empty file, considered as a sh program?


I like this Python3 quine, which doesn't use repr printing:

  (lambda s,q,b,k,c,e:print(b+s+k+b+q+s+q+c+q+e+q+q+c+q+b+q+c+q+k+q+c+q+c+q+c+q+e+e+q+k))("lambda s,q,b,k,c,e:print(b+s+k+b+q+s+q+c+q+e+q+q+c+q+b+q+c+q+k+q+c+q+c+q+c+q+e+e+q+k)","\"","(",")",",","\\")


I'd say a boring quine would be one around the empty file. I bet there's enough lenient scripting languages to arguably make a decently sized quine.


There is nothing boring about this quine, that is the general approach that works in any language


Sounds like the blank file was already a Quine!

Best regards - Douglas Quine Wwquine.org


Wvquine.org (correcting typo)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: