# Mathematica quine

A quine is a computer program which takes no input and produces a copy of its own source code as its only output. There are many trivial quines in Mathematica:

In[1]:= "Hello world"
Out[1]= "Hello world"

In[2]:= 3.14
Out[2]= 3.14

In[3]:= f[x]
Out[3]= f[x]


where f and x are undefined symbols; and some more

In[4]:= Hold[N[\[Pi]]]
Out[4]= Hold[N[\[Pi]]]


These are all trivial. I was thinking that perhaps a more interesting challenge for Mathematica was a multiquine. This is a program A that outputs another program B, distinct from A, such that when B is exectued, the output is A. Multiple levels of depth are also allowed: Thus one might have a program that when executed outputs a distinct program that when executed outputs another program distinct from the first two ... that when executed outputs the original program.

There are also multiquines that output a distinct program in a different language, such that when this program is executed, the output is the original program.

QUESTION (Though more of a challenge): Can you come up with a multiquine for Mathematica?

-

Here is a 24 character nontrivial solution:

Print[ToString[#0][]] & []

-
Sorry, that's not a Quine. However adding a space after the & makes it one. –  celtschk May 28 '12 at 8:22
Thanks, just did. –  M.R. May 28 '12 at 16:42
It doesn't work as I'd expect it to. When I execute Print[ToString[#0][]] & [], the output is "Print[ToString[#0][]] & "[], which doesn't equal the original expression. Then if you execute "Print[ToString[#0][]] & "[], you get "Print[ToString[#0][]] & "[]. Therefore the program "Print[ToString[#0][]] & "[] is a quine. However, Print[ToString[#0][]] & [] is neither a quine nor a multiquine. –  becko May 28 '12 at 18:05
@IstvánZachar Good point. I think that the output's InputForm should equal the original quine source code (or the output of the output ... for a multiquine), but I am open to suggestions. As far as I know a established definition of what a quine is in the particular case of Mathematica doesn't exist, and as your comment exemplifies, there are subtleties that distinguish Mathematica from other languages. Part of the knowledge I expect to gain from this question is a definition of Mathematica quine that makes the problem non-trivial and interesting. –  becko May 28 '12 at 18:50
Well, start the kernel (without the front end), enter the code (which consists of a series of characters, of course) and look at the produced output (which is again a series of characters, sent to standard output). Or alternatively, you could also enter the code in the Front end, but then it would have to produce an identical cell (i.e. the same box structure). The internal form of the expression doesn't matter (except as far as it affects the output). –  celtschk May 29 '12 at 8:23

Something like this would work?

Module[{$guard = True}, f[i_List] /;$guard :=
Block[{$guard = False, par = RotateRight[i]}, g[par]]; g[i_List] /;$guard :=
Block[{\$guard = False, par = RotateRight[i]}, f[par]]
]


So

f[Range[10]]

g[{9, 10, 1, 2, 3, 4, 5, 6, 7, 8}]

f[{7, 8, 9, 10, 1, 2, 3, 4, 5, 6}]

...


Another option

#2[#0[! #, #2]] &[!True, Defer]

-
I think that prior definitions should be avoided, so your first example is not what I had in mind. The second example is more like it. But I think you should change True to !False. Then you truly have a 2-level multiquine. –  becko May 28 '12 at 18:53
@becko, if what you had in mind is code that returns some cell that if evaluated as is, returns some other etc etc in a loop, without prior definitions, it probably can only be done by messing with the parsing stage, as I do here with Defer. The kernel will always return evaluated expressions I think unless you use the trick/bug on my first example, which isn't what you were looking for –  Rojo May 28 '12 at 20:52
@becko, thanks, edited to !True –  Rojo May 29 '12 at 1:04
I'm pretty sure #0 is cheating. It's like fopen("program.c", "r") in C. –  nikie Jan 7 at 21:15

I have written several quines in Mathematica that you might appreciate.

Here is one solution:

quine[x_String] :=   Print[x, ";", FromCharacterCode[10] , "quine[", InputForm[x],    "]"];
quine["quine[x_String]:=Print[x,\";\",FromCharacterCode[10],\"\ quine[\",InputForm[x]],\"]\""]


And here is a much more fun one, which is a little hard to display in StackExchange,

where that tiny text is a bitmap that reads

The TextRecognize[] command doesn't do well with non-dictionary words, which means it typically does poorly with Mathematica code. I found that it recognized a bitmap of "quine" as "guine" in every font I tested, so I just renamed the function accordingly.

These are not multiquines, but aficionados should like them nonetheless.

-

Self-rotating code:

(Interpretation[Rotate[R, 2 π #/5], #0[Mod[#, 5] + 1]] &)[0]


It produces R which rotates by 2 π/5 after each evaluation

Full input form of this symbol is

Interpretation[Rotate[R, (2*Pi)/5],
(Interpretation[Rotate[R, 2*Pi*(#1/5)], #0[Mod[#1, 5] + 1]] & )[
Mod[1, 5] + 1]]


Don't drink and derive! :)

You can copy-pasting result of one self-rotating code to another. Full code is here.

-

I think this is the most straightforward way to translate the canonical quine of "Print the following twice, the second time in quotes" into Mathematica.

With[{a=FromCharacterCode@34},Print[#<>a<>#<>a]]&@"With[{a=FromCharacterCode@34},Print[#<>a<>#<>a]]&@"

-