[net.sources] Thompson's self-reproducing program

sloane@noscvax.UUCP (Gary K. Sloane) (08/23/84)

Compile this program as:  cc -o s s.c


main()
{
system("cat s.c");
}


My friend Steve suggests the following alias in your .login:

		alias cat 'c \!*'

With the obvious byte-reducing benefits to s.c when modified accordingly...


    * **************************************************************** *
    * Gary K. Sloane/Computer Sciences Corporation/San Diego, CA 92110 *
    * DDD: (619) 225-8401                                              *
    * MILNET: sloane@nosc                                              *
    * UUCP: [ihnp4,akgua,decvax,dcdwest,ucbvax]!sdcsvax!noscvax!sloane *
    * **************************************************************** *

alexc@dartvax.UUCP (Alex Colvin) (08/28/84)

"What is the point of a self-duplicating program?"

Well ... For one thing, you never have to keep the source code around.

Seriously, it's mildly interesting as a bit-banging (character-banging)
challenge to those who don't worry about the fixed-point combinator.
For those who do, here's a different version in SCHEME (or, with slight
syntax changes, in LISP).

(labels (f (lambda () f)) f)

This one simply returns itself -- not necessarily its source code.  It gets
printed by the read-eval-print loop.  A C analogue is 

f() { return f; };