www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Quine using strings?

reply Nestor <nestorperez2016 yopmail.com> writes:
I was reading some of the examples of writing a quine with D, but 
apparently the language has evolved and they no longer compiled 
unchanged.

So I tried to program one by myself using strings and std.stdio, 
but the result seems long and redundant:

import std.stdio;void main(){string s=`import std.stdio;void 
main(){string 
s=writefln("%s\x60%s\x60;s",s[0..38],s,s[38..$]);}`;writefln("%s\x60%s\x60;%s",s[0..38],s,s[38..$]);}

Any ideas for a shorter version (preferably without using 
pointers)?
Jan 15
next sibling parent reply Nestor <nestorperez2016 yopmail.com> writes:
On Sunday, 15 January 2017 at 19:43:22 UTC, Nestor wrote:
 I was reading some of the examples of writing a quine with D, 
 but apparently the language has evolved and they no longer 
 compiled unchanged.

 So I tried to program one by myself using strings and 
 std.stdio, but the result seems long and redundant:

 import std.stdio;void main(){string s=`import std.stdio;void 
 main(){string 
 s=writefln("%s\x60%s\x60;s",s[0..38],s,s[38..$]);}`;writefln("%s\x60%s\x60;%s",s[0..38],s,s[38..$]);}

 Any ideas for a shorter version (preferably without using 
 pointers)?
Well I just noticed a few problems in the code, I guess quines are a little complex to write.
Jan 15
parent reply pineapple <meapineapple gmail.com> writes:
On Sunday, 15 January 2017 at 21:37:53 UTC, Nestor wrote:
 Any ideas for a shorter version (preferably without using 
 pointers)?
When compiling with the -main flag, this D program is a quine:
Jan 15
parent reply Nestor <nestorperez2016 yopmail.com> writes:
On Sunday, 15 January 2017 at 22:08:47 UTC, pineapple wrote:
 On Sunday, 15 January 2017 at 21:37:53 UTC, Nestor wrote:
 Any ideas for a shorter version (preferably without using 
 pointers)?
When compiling with the -main flag, this D program is a quine:
You forgot to include the program... or is this a joke? ;)
Jan 15
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 15 January 2017 at 22:35:26 UTC, Nestor wrote:
 You forgot to include the program... or is this a joke? ;)
Neither: the empty program compiles and runs, outputting nothing. Since its empty output matches its empty source file, it technically fits the definition of the quine :)
Jan 15
prev sibling next sibling parent Michael Coulombe <kirsybuu gmail.com> writes:
A quine I came up with a while ago, using q{} string notation:

enum s = q{enum s = q{%s};
void main() {
     import std.stdio;
     writefln(s,s);
}};
void main() {
     import std.stdio;
     writefln(s,s);
}
Jan 15
prev sibling parent reply Basile B. <b2.temp gmx.com> writes:
On Sunday, 15 January 2017 at 19:43:22 UTC, Nestor wrote:
 I was reading some of the examples of writing a quine with D, 
 but apparently the language has evolved and they no longer 
 compiled unchanged.

 So I tried to program one by myself using strings and 
 std.stdio, but the result seems long and redundant:

 import std.stdio;void main(){string s=`import std.stdio;void 
 main(){string 
 s=writefln("%s\x60%s\x60;s",s[0..38],s,s[38..$]);}`;writefln("%s\x60%s\x60;%s",s[0..38],s,s[38..$]);}

 Any ideas for a shorter version (preferably without using 
 pointers)?
I remember on Rosetta to have seen this: module quine; import std.stdio; void main(string[] args) { write(import("quine.d")); } compiles with: dmd path/quine.d -Jpath
Jan 15
parent reply Nestor <nestorperez2016 yopmail.com> writes:
On Monday, 16 January 2017 at 06:41:50 UTC, Basile B. wrote:
 I remember on Rosetta to have seen this:

 module quine;
 import std.stdio;
 void main(string[] args)
 {
     write(import("quine.d"));
 }

 compiles with: dmd path/quine.d -Jpath
Very good! By the way, module name and arguments aren't needed, so: import std.stdio;void main(){write(import("q.d"));} compile with: "dmd q -J." PS. Isn't this approach considered "cheating" in quines? ;)
Jan 16
parent reply pineapple <meapineapple gmail.com> writes:
On Monday, 16 January 2017 at 09:33:23 UTC, Nestor wrote:
 PS. Isn't this approach considered "cheating" in quines? ;)
I'm afraid so - while the empty program has been technically accepted as being a quine (e.g. http://www.ioccc.org/1994/smr.hint) programs which use file io to read their own source have not.
Jan 16
parent Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
On Monday, 16 January 2017 at 13:11:38 UTC, pineapple wrote:
 On Monday, 16 January 2017 at 09:33:23 UTC, Nestor wrote:
 PS. Isn't this approach considered "cheating" in quines? ;)
I'm afraid so - while the empty program has been technically accepted as being a quine (e.g. http://www.ioccc.org/1994/smr.hint) programs which use file io to read their own source have not.
But the program doesn't read from IO it's own program. It is a stand alone executable which does no file IO. The compiler on the other hand does IO to inject the file into the program.
Jan 17