[net.misc] self-printing programs

oscar@utcsrgv.UUCP (Oscar M. Nierstrasz) (12/14/83)

Here's a puzzle!  (Perhaps this belongs in net.jokes ...)

What is the shortest program you can come up with whose only output is
the program itself?

Trivial solutions are not acceptable -- the null program will obviously
do the trick.  Similarly, it must not access any external text or data
files since the shell script

	cat $0

simply reads and prints itself.  Clearly all the "data" must be
contained in the program itself.  A shell script containing

	self

where "self" is a shell script that does an "echo self" is also
cheating.

So far I have a three-line shell script that seems to fit the bill.
Any takers for this mind-bender?  Please *mail* me and I'll post the
cleverest solutions after a decent wait.  (Programs are fine, but shell
scripts tend to be shorter.)

Oscar Nierstrasz
-- 

UUCP:	{ allegra cornell decvax decwrl floyd ihnp4 linus
	  sask ubc-vision utzoo uw-beaver watmath } !utcsrgv!oscar

oscar@utcsrgv.UUCP (Oscar M. Nierstrasz) (12/14/83)

I posted an article yesterday asking people for self-contained,
self-printing programs.  Apparently this has already been dealt
with in net.lang.c -- rlgvax!jack has wonderful 3-liner in C.
My own shell solution was:

	sed -e p -e p -e s/.*/!/ <<!
	sed -e p -e p -e s/.*/!/ <<!
	!

So much for that idea.

Oscar Nierstrasz
-- 

UUCP:	{ allegra cornell decvax decwrl floyd ihnp4 linus
	  sask ubc-vision utzoo uw-beaver watmath } !utcsrgv!oscar

root@zehntel.UUCP (12/23/83)

#R:utcsrgv:-293300:zinfandel:8200063:000:245
zinfandel!berry    Dec 22 11:23:00 1983

Here is a one line c program that prints itself:  It's 101 chars.

main(){char q=34,n=10,*a="main(){char q=34,n=10,*a=%c%s%c;printf(a,q,a,q,n);}%c";printf(a,q,a,q,n);}

Berry Kercheval		Zehntel Inc.	(ihnp4!zehntel!zinfandel!berry)
(415)932-6900