|
|
This is the self-printing program item. Post programs that print out their own source code (in whatever language) here.
23 responses total.
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);
}
#!/bin/cat
what's the point of #define ing PRT ?
You tell me. :-)
i guess the # flag assures that everything is printed ?
Sortof. It "stringifies" its argument.
i'll have to look at this again when i'm not writing cantus firmi and memorising terms like "complément circonstanciel de temps".
Take your time.
that's the problem with being a full-time student; there's never enough time :(
True.
Google for "quine".
Interesting. I found this nice page over at Nyx: http://www.nyx.net/~gthompso/quine.htm
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.
Ooo...cheater.
I prefer to think of it as efficiency and elegant simplicity.
The question is, what language is it in?
it's a quine in just about any shell you care to interpret it with..
I was leading up to a joke about the empty set, but I'll refrain.
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"
all right ; BASIC
I've already spotted a typo in it. Line 160 should match line 30, but doesn't.
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"
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.
|
|
- Backtalk version 1.3.30 - Copyright 1996-2006, Jan Wolter and Steve Weiss