@@CloneMe SG From: tedb@hpcvra.cv.hp.com. (Ted Beers) Newsgroups: comp.sys.hp48 Subject: HP 48 Program Procreation Date: 23 Aug 91 22:06:57 GMT Organization: Hewlett-Packard Co., Corvallis, OR, USA I was intrigued by a challenge made by Ken Thompson (coauthor of Unix) many years ago and mentioned again in the September, 1991, issue of Dr. Dobb's Journal. To paraphrase, as a test of a computer language's power, Thompson suggested that one write a program that, when compiled and executed, will produce an exact copy of itself. Taking up the gauntlet, here's what I came up with for the HP 48: Warning! Examining this program too closely can prove hazardous to your sanity. @ CloneMe: A program that creates itself. @ Reference: Dr. Dobb's Journal #180, September, 1991, @ pg. 18. @ Problem first proposed by Ken Thompson. @ \<< C$ 31 "\<< C$ 31 " OVER + SWAP + STR\-> \>> "\<< C$ 31 " OVER + SWAP + STR\-> \>> (Note: C$ is a little-known built-in object delimiter for an -character string.) When this program is executed, it will output itself! At first this exercise might seem either trivial or impossible, but it's neither. The trick at work here is to make the first half of the program create a string that represents the second half of the program, and to make the second half of the program put together the first half of the program with the second half and change the result into a program (whew!). Variations are possible. Here's a program that creates TWO copies of itself, changing the motive from procreation to proliferation: \<< C$ 35 "\<< C$ 35 " OVER + SWAP + STR\-> DUP \>> "\<< C$ 35 " OVER + SWAP + STR\-> DUP \>> And then of course there's the suicidal variation who's sole purpose is to disappear: \<< \>> Can anyone create a shorter version of the program that creates itself? (Removing unnecessary but friendly formatting doesn't count!) Ted W. Beers Hewlett-Packard