|
Grex > Jelly > #48: The Self-Printing Program item | |
|
| Author |
Message |
cross
|
|
The Self-Printing Program item
|
Sep 22 19:22 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. |
cross
|
|
response 1 of 23:
|
Sep 22 19:24 UTC 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);
}
|
kingjon
|
|
response 2 of 23:
|
Sep 22 21:32 UTC 2006 |
#!/bin/cat
|
naftee
|
|
response 3 of 23:
|
Sep 22 22:33 UTC 2006 |
what's the point of #define ing PRT ?
|
cross
|
|
response 4 of 23:
|
Sep 22 22:39 UTC 2006 |
You tell me. :-)
|
naftee
|
|
response 5 of 23:
|
Sep 23 00:48 UTC 2006 |
i guess the # flag assures that everything is printed ?
|
cross
|
|
response 6 of 23:
|
Sep 23 00:49 UTC 2006 |
Sortof. It "stringifies" its argument.
|
naftee
|
|
response 7 of 23:
|
Sep 24 05:15 UTC 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".
|
cross
|
|
response 8 of 23:
|
Sep 24 05:23 UTC 2006 |
Take your time.
|
naftee
|
|
response 9 of 23:
|
Sep 24 05:33 UTC 2006 |
that's the problem with being a full-time student; there's never enough time
:(
|
cross
|
|
response 10 of 23:
|
Sep 24 05:34 UTC 2006 |
True.
|
sholmes
|
|
response 11 of 23:
|
Sep 24 08:14 UTC 2006 |
Google for "quine".
|
cross
|
|
response 12 of 23:
|
Sep 24 15:59 UTC 2006 |
Interesting. I found this nice page over at Nyx:
http://www.nyx.net/~gthompso/quine.htm
|
mcnally
|
|
response 13 of 23:
|
Sep 25 16:22 UTC 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.
|
cross
|
|
response 14 of 23:
|
Sep 25 17:20 UTC 2006 |
Ooo...cheater.
|
mcnally
|
|
response 15 of 23:
|
Sep 25 18:34 UTC 2006 |
I prefer to think of it as efficiency and elegant simplicity.
|
cross
|
|
response 16 of 23:
|
Sep 25 22:35 UTC 2006 |
The question is, what language is it in?
|
mcnally
|
|
response 17 of 23:
|
Sep 26 02:29 UTC 2006 |
it's a quine in just about any shell you care to interpret it with..
|
cross
|
|
response 18 of 23:
|
Sep 26 03:43 UTC 2006 |
I was leading up to a joke about the empty set, but I'll refrain.
|
gull
|
|
response 19 of 23:
|
Oct 2 22:44 UTC 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"
|
naftee
|
|
response 20 of 23:
|
Oct 4 03:58 UTC 2006 |
all right ; BASIC
|
gull
|
|
response 21 of 23:
|
Oct 4 04:37 UTC 2006 |
I've already spotted a typo in it. Line 160 should match line 30, but
doesn't.
|
blaise
|
|
response 22 of 23:
|
Oct 4 16:48 UTC 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"
|
cross
|
|
response 23 of 23:
|
Sep 7 08:06 UTC 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
|