Well, I don't know about F#, but in PostScript you can do:
/quine {
[ exch {exec [} aload pop
[ 3 index
{exec [ aload 7 1 roll ] cvx} aload pop
] cvx
{aload 7 1 roll ] cvx} aload pop
] cvx
} def
This is a quinemaker, which when applied to a procedure, returns an executable quine, which is analogous to the conventional quine (a program that prints out its own source code). The executable quine does something and returns itself. Like the conventional quine, in which referencing the filename of the source code is considered cheating, the executable quine would be cheating if it referenced its own name within its body.
Since the above doesn't use any variables at all, not even locally, it makes legit quines. ^_^ (Of course, local variables are acceptable, but you don't always need them in PostScript.) It seems that almost all of the other solutions to this question are cheating by using named recursion or the this
construct. :/
You can give this a procedure such as count
and then exec
the output any number of times:
{count} quine % make an executable quine
exec exec exec exec exec exec exec exec % execute it eight times
pop pstack % remove it from stack and print stack
This program prints out the integers from 7 to 0, counting down. There aren't even any numbers in the program source.
Executable quines for the win!
The equivalent for your example would be
(awesome!) (its) (useless) (so) (is) (this)
{=} quine
exec exec exec exec exec exec
As a bonus, here is a recursive infinite loop in PostScript (the iterative version is just {} loop
):
[{[aload 8 1 roll] cvx exec} aload 8 1 roll] cvx exec
To stream an infinite list of pseudorandom integers to stdout, do
[{rand = [aload 8 1 roll] cvx exec} aload 8 1 roll] cvx exec
In fact, this infinite loop is nothing more than an auto-executing executable quine. So we can modify the quinemaker to do this:
/autoexecquine {
[ exch {exec [} aload pop
[ 3 index
{exec [ aload 8 1 roll ] cvx exec} aload pop
] cvx
{aload 8 1 roll ] cvx exec} aload pop
] cvx
} def
Then it's possible to run printer("this")("is")("so")("useless")("its")("awesome!")
as PostScript code. Yes, in prefix style. You didn't think prefix style was possible in PostScript? Think again.
/printer
{
{currentfile token {1 1 index length 2 sub getinterval =}{quit} ifelse}
autoexecquine exec
} def
printer("this")("is")("so")("useless")("its")("awesome!")
Running this in the GhostScript shell, after defining autoexecquine
and printer
:
GS>printer("this")("is")("so")("useless")("its")("awesome!")
this
is
so
useless
its
awesome!
Note that this is a hack that reads data from the current file (stuff not yet processed) and operates on it, instead of what's already on the stack. It won't work when embedded in a function.