No Next Item No Next Conference Can't Favor Can't Forget Item List Conference Home Entrance    Help
View Responses


Grex Systems Item 48: The Self-Printing Program item
Entered by cross on Fri Sep 22 19:22:21 UTC 2006:

This is the self-printing program item.  Post programs that print out their
own source code (in whatever language) here.

23 responses total.



#1 of 23 by cross on Fri Sep 22 19:24:58 2006:

Here's my first example, in C.  Let's see if fronttalk doesn't eat it up....
Hopefully, I didn't make any errors cutting and pasting it.

/*
 *  This is a test.
 */

#include <stdio.h>
#include <stdlib.h>
#define PRT(S)  (void)printf(S, #S)

int
main(void)
{
        PRT("/*\n *  This is a test.\n */\n\n#include <stdio.h>\n#include
        <stdlib.h>\n#define\tPRT(S)\t(void)printf(S,
        #S)\n\nint\nmain(void)\n{\n\tPRT(%s);\n\treturn(EXIT_SUCCESS);\n}\n");
               return(EXIT_SUCCESS);
}


#2 of 23 by kingjon on Fri Sep 22 21:32:57 2006:

#!/bin/cat



#3 of 23 by naftee on Fri Sep 22 22:33:37 2006:

what's the point of #define ing PRT ?


#4 of 23 by cross on Fri Sep 22 22:39:59 2006:

You tell me.  :-)


#5 of 23 by naftee on Sat Sep 23 00:48:51 2006:

i guess the # flag assures that everything is printed ? 


#6 of 23 by cross on Sat Sep 23 00:49:15 2006:

Sortof.  It "stringifies" its argument.


#7 of 23 by naftee on Sun Sep 24 05:15:31 2006:

i'll have to look at this again when i'm not writing cantus firmi and
memorising terms like "complément circonstanciel de temps".


#8 of 23 by cross on Sun Sep 24 05:23:46 2006:

Take your time.


#9 of 23 by naftee on Sun Sep 24 05:33:21 2006:

that's the problem with being a full-time student; there's never enough time
 :(


#10 of 23 by cross on Sun Sep 24 05:34:19 2006:

True.


#11 of 23 by sholmes on Sun Sep 24 08:14:20 2006:

Google for "quine".


#12 of 23 by cross on Sun Sep 24 15:59:27 2006:

Interesting.  I found this nice page over at Nyx:
http://www.nyx.net/~gthompso/quine.htm


#13 of 23 by mcnally on Mon Sep 25 16:22:01 2006:

 Well, I was going to enter a null response but there's no easy way to
 do it that I'm aware.  But it would satisfy the requirements.


#14 of 23 by cross on Mon Sep 25 17:20:44 2006:

Ooo...cheater.


#15 of 23 by mcnally on Mon Sep 25 18:34:44 2006:

 I prefer to think of it as efficiency and elegant simplicity.


#16 of 23 by cross on Mon Sep 25 22:35:26 2006:

The question is, what language is it in?


#17 of 23 by mcnally on Tue Sep 26 02:29:04 2006:

 it's a quine in just about any shell you care to interpret it with..


#18 of 23 by cross on Tue Sep 26 03:43:33 2006:

I was leading up to a joke about the empty set, but I'll refrain.


#19 of 23 by gull on Mon Oct 2 22:44:45 2006:

Here's one in BASIC.  It has to jump through some hoops to get around 
the inability to escape quotation marks in most BASICs.  I don't have a 
BASIC interpreter handy to test it, but I think it should work:

 10 READ N
 20 READ D$
 30 FOR X = 1 TO (N-2)
 40 READ A$
 50 PRINT X*10 A$
 60 NEXT X
 70 RESTORE
 80 FOR Y = 1 TO N
 90 READ A$
100 PRINT (X+Y)*10 D$ CHR$(34) A$ CHR$(34)
110 NEXT Y
120 DATA 13
130 DATA "DATA "
140 DATA "READ N"
150 DATA "READ D$"
160 DATA "FOR X = 1 TO N"
170 DATA "READ A$"
180 DATA "PRINT X*10 A$"
190 DATA "NEXT X"
200 DATA "RESTORE"
210 DATA "FOR Y = 1 TO N"
220 DATA "READ A$"
230 DATA "PRINT (X+Y)*10 D$ CHR$(34) A$ CHR$(34)"
240 DATA "NEXT Y"


#20 of 23 by naftee on Wed Oct 4 03:58:55 2006:

all right ; BASIC


#21 of 23 by gull on Wed Oct 4 04:37:01 2006:

I've already spotted a typo in it.  Line 160 should match line 30, but 
doesn't.


#22 of 23 by blaise on Wed Oct 4 16:48:35 2006:

There were a couple of problems with it.  It would also print line 120
as '120 DATA "13"'.  Here's a corrected version; I believe it will work,
but also don't have a BASIC interpreter available.

 10 READ N
 20 READ D$
 30 FOR X = 1 TO N-1
 40 READ A$
 50 PRINT X*10 A$
 60 NEXT X
 70 RESTORE
 80 READ N
 90 PRINT N*10 D$ N
100 FOR Y = 1 TO N
110 READ A$
120 PRINT (N+Y)*10 D$ CHR$(34) A$ CHR$(34)
130 NEXT Y
140 DATA 14
150 DATA "DATA "
160 DATA "READ N"
170 DATA "READ D$"
180 DATA "FOR X = 1 TO N-1"
190 DATA "READ A$"
200 DATA "PRINT X*10 A$"
210 DATA "NEXT X"
220 DATA "RESTORE"
230 DATA "READ N"
240 DATA "PRINT N*10 D$ N"
250 DATA "FOR Y = 1 TO N"
260 DATA "READ A$"
270 DATA "PRINT (N+Y)*10 D$ CHR$(34) A$ CHR$(34)"
280 DATA "NEXT Y"


#23 of 23 by cross on Tue Sep 7 08:06:03 2010:

An interesting post that reminded me of this item:  Self-reproducing 
zip and gzip files:

http://research.swtch.com/2010/03/zip-files-all-way-down.html

Response not possible - You must register and login before posting.

No Next Item No Next Conference Can't Favor Can't Forget Item List Conference Home Entrance    Help

- Backtalk version 1.3.30 - Copyright 1996-2006, Jan Wolter and Steve Weiss