Path: utzoo!mnetor!uunet!mcvax!prlb2!kulcs!luc From: luc@kulcs.UUCP (Luc Van Braekel) Newsgroups: comp.lang.pascal Subject: Re: self-replicating programs? Message-ID: <1070@kulcs.UUCP> Date: 9 Dec 87 08:31:27 GMT References: <1400@tulum.swatsun.UUCP> Organization: Kath.Univ.Leuven, Comp. Sc., Belgium Lines: 37 Summary: here is a self-replicating pascal program In article <1400@tulum.swatsun.UUCP>, hirai@swatsun (Eiji "A.G." Hirai) writes: > In our recent ACM programming contest (regionals), one of the > problems was to write a self-replicating program. That is, we had to > write a program whose output was itself, the source code. No alterations > of the original code during execution was allowed (I think). > Does anyone have any code for this problem? We have one but > it looks inelegant. I've also see bery bery short Prolog code for this. > Help, we are looking for good codes to study! And yes, the contest is > over (we ain't cheating). Here is a self-replicating Pascal program I wrote a few years ago. The program looks dirty but it works ! program self (output); var i,j: integer; a: array[1..8] of packed array[1..59] of char; begin a[1] := 'program self (output); '; a[2] := 'var i,j: integer; '; a[3] := ' a: array[1..8] of packed array[1..59] of char; begin '; a[4] := 'for i := 1 to 3 do writeln(a[i]); '; a[5] := 'for i := 1 to 8 do begin write('' a['',i:0,''] := '',chr(39));'; a[6] := 'for j := 1 to 59 do begin write(a[i][j]);if a[i][j]=chr(39)'; a[7] := 'then write(a[i][j]) end; writeln(chr(39),'';'') end; '; a[8] := 'for i := 4 to 8 do writeln(a[i]) end. '; for i := 1 to 3 do writeln(a[i]); for i := 1 to 8 do begin write(' a[',i:0,'] := ',chr(39)); for j := 1 to 59 do begin write(a[i][j]);if a[i][j]=chr(39) then write(a[i][j]) end; writeln(chr(39),';') end; for i := 4 to 8 do writeln(a[i]) end. +-----------------------------------+------------------------------------+ | Name : Luc Van Braekel | Katholieke Universiteit Leuven | | UUCP : luc@kulcs.UUCP | Department of Computer Science | | BITNET : luc@blekul60.bitnet | Celestijnenlaan 200 A | | Phone : +(32) 16 20 0656 x3563 | B-3030 Leuven (Heverlee) | | Telex : 23674 kuleuv b | Belgium | +-----------------------------------+------------------------------------+