
How to Generate Self-Referential Programs - luu
http://malisper.me/2016/04/20/writing-self-referential-programs/
======
tyingq
Usually called a "Quine", right?

Here's a JavaScript one that's pretty neat:
[http://aem1k.com/world/](http://aem1k.com/world/)

------
ivanbakel
It struck me that this ends up looking a lot like the Y-combinator, which
makes sense, since it's a function that acts on itself without a recursive
definition. Pretty cool to see the overlap of simple concepts in various ways.
I've always wondered how quines worked on general principle.

------
Smaug123
This definitely feels like cheating. There's so much one could do rather than
just use the ` operator to quote the program. Given a program f, one can
create a program g which prints out g and then does f, and one does not
require any special operators to do this. There's some cool computability
stuff in this area, and I don't know (off the top of my head) of a readable
introduction.

~~~
asrp
I agree. Using quote is basically using the Lisp parser to do the work and is
almost like reading the source program as input. It would have been
interesting if their program contained a parser (which I think wouldn't be too
distracting because of homoiconicity).

I found this an interesting read anyways especially when they start
parametrizing the quine by a function F near the end. Although my interest
shouldn't be that much of a surprise given my username:

asrp: a self-referential person/program/place

~~~
kazinator
Doug H, is that you?

------
gtirloni
Can anyone give an example of a real world application that uses this
technique? Why would you do it?

~~~
travmatt
I'd imagine this is very useful for writing self-modifying or polymorphic
code.

~~~
taneq
And anything using this technique would be a perfect illustration of the fact
that code is harder to debug than to write, so if you write the cleverest code
you can write, you are by definition not smart enough to debug it.

~~~
travmatt
""" On Self-Modifying Code and the Space Shuttle OS

I was doing some reading about Metaprogramming and Self-modifying code at
Wikipedia, a fascinating topic with many uses from optimization, patching, and
genetic programming.

And it reminded me of my days during the early 1990s working as a software
engineer on the Space Shuttle operating system (FCOS). Many people don’t know
that the Space Shuttle OS implements self-modifying code for the purpose of
“fault-tolerance”.

...

An alternative to self-modifying or patching code is to use conditions (if
statements) and local store (such as in-memory) to store flags that are used
determine the paths to take (assuming such paths are known ahead of time,
which might not be the case on genetic programming). Being the Space Shuttle a
memory-limited, non-dynamic memory map and management environment, it is
cheaper, less complex, and safer to patch specific instructions directly. """

[http://weblog.cenriqueortiz.com/computing/2007/08/18/on-
self...](http://weblog.cenriqueortiz.com/computing/2007/08/18/on-self-
modifying-code-and-the-space-shuttle-os/)

It sounds useful in the appropriate contexts, much like every other technique.

