SRL Archive - General

Quines in Simba


fastler

2014-11-30 10:56:07


Quines:
[INDENT]What are they?[/INDENT]
[INDENT][INDENT]Quines, in short, are curious pieces of code that can write themselves back character for character.
For further reading check out: Wikipedia and this collection The Quine Page[/INDENT][/INDENT]

[INDENT]Why should you care?[/INDENT]
[INDENT][INDENT]Because.[/INDENT][/INDENT]
[INDENT]What does it look like?[/INDENT]
[INDENT][INDENT]Brace yourself. This is the compactest one I could find, at the time of writing, that works in Simba:[/INDENT][/INDENT]
[INDENT][INDENT]
const a='const a=';b='begin writeln(a,#39,a,#39#59#98#61#39,b,#39#59#10,b) end.';
begin writeln(a,#39,a,#39#59#98#61#39,b,#39#59#10,b) end.
[/INDENT][/INDENT]
[INDENT][INDENT]Credit: Geoffrey A Swift (blimey@toke.com)
Note: I changed write() to writeln()[/INDENT][/INDENT]

[INDENT]What is that?[/INDENT]
[INDENT][INDENT]It's a Quine! Let me break it down piece by piece so we can better understand how it works.
Even I at the time of writing this don't know what's exactly going on. Soon enough all shall be made clear.

Right from the start two constants are declared,
a:
const a='const a=';
and b:
b='begin writeln(a,#39,a,#39#59#98#61#39,b,#39#59#10,b) end.';

"a" is the "initial" part of the script and "b" is the "body+end" of the script.

The "body+end" of the script is then actually executed on the next line with:
begin writeln(a,#39,a,#39#59#98#61#39,b,#39#59#10,b) end.
This outputs the constants a and b in the appropriate locations and
any additional characters needed in between to recreate the script.
And let's not forget the important "end." to "finalize" the code.

For quick reference:
39 is '
59 is ;
98 is b
61 is =
10 is new line feed
[/INDENT][/INDENT]

[INDENT]Now what?[/INDENT]
[INDENT][INDENT]Well, I guess that's it. But nothing is stopping you from attempting to make it even shorter.
Or perhaps write your own! There are an infinite number of ways to make a Quine! Shortness isn't a requirement.
Some people make their Quines ridiculously complex just for the heck of it, sometimes even over several programming languages.
If you have one to share, post it below![/INDENT][/INDENT]


fastler

2014-11-30 10:57:07

I put the compact Quine onto one line, shortening the total character count by 6 (newline noninclusive):
const a='const a=';b='begin writeln(a,#39,a,#39#59#98#61#39,b,#39#59,b) end.';begin writeln(a,#39,a,#39#59#98#61#39,b,#39#59,b) end.

I also put it in a form using only one constant instead of two, but it's longer by 3 characters:
const a='begin writeln(#99#111#110#115#116#32#97#61#39,a,#39#59,a) end.';begin writeln(#99#111#110#115#116#32#97#61#39,a,#39#59,a) end.

KeepBotting

2014-11-30 12:11:15

So wait, this is code that writes code?

o_O

Hoodz

2014-11-30 12:21:19

@Ashaman88; script-writer-script leak?!?!

Brandon

2014-11-30 12:25:34

From: KeepBotting
So wait, this is code that writes code?

o_O


Nope. It's code that prints itself. A program that prints its own source code..

Clarity

2014-11-30 13:25:22

From: Brandon
Nope. It's code that prints itself. A program that prints its own source code..

Forgive me if I'm being ignorant, but what is the purpose of this? Or is it just for sh*ts and giggles?

rj

2014-11-30 13:42:32

trol-lol-lol-lol-lol

lol-lol-lol-LA

function getScriptCode():string;
var
h:integer;
begin
h := openfile(scriptPath + scriptFile, true);
readFileString(h, result, fileSize(h));
closefile(h);
end;

Theoretically we could make the script run itself infinite number of times with the correct cmd arguments to run a simba script:

function getScriptCode():string;
var
h:integer;
begin
h := openfile(scriptPath + scriptFile, true);
readFileString(h, result, fileSize(h));
closefile(h);
end;

function duplicateScript():string;
var
fName, fContents:string;
fNum:integer;
begin
fName := toStr(random(high(int64)));
fContents := getScriptCode();
fNum := createFile(scriptPath + fName + '.simba');
closeFile(fNum);
fNum := reWriteFile(scriptPath + fName + '.simba', true);
writeFileString(fNum, fContents);
closeFile(fNum);
result := scriptPath + fName + '.simba';
end;

begin
openWebPage('start ' + duplicateScript + ' -run');
end.

fastler

2014-11-30 13:49:18

From: Clarity
Forgive me if I'm being ignorant, but what is the purpose of this? Or is it just for sh*ts and giggles?


I should probably add more than "Because" to the "Why should you care"…
As far as I understand it's just for sh*ggles, but it's also a display of the language being Turing complete.


From: Robert
trol-lol-lol-lol-lol

lol-lol-lol-LA

function getScriptCode():string;
var
h:integer;
begin
h := openfile(scriptPath + scriptFile, true);
readFileString(h, result, fileSize(h));
closefile(h);
end;

Ideally you aren't allowed to just read the code from somewhere, especially the source file.
But if you can chop it down to an impressive shortness then it's something. ^_^
If you were to go full troll then an empty program would be the shortest.
By not outputting anything it has thereby outputted itself.
I hope that doesn't spiral off into a philosophical debate on the definition of nothing.

akarigar

2014-11-30 16:28:50

That isn't full troll, it's just incorrect. Pretty sure a quine can't be empty. Quines are only good to learn more about the language and it can be fun trying to find the shortest one.

fastler

2014-11-30 17:33:51

From: akarigar
That isn't full troll, it's just incorrect. Pretty sure a quine can't be empty. Quines are only good to learn more about the language and it can be fun trying to find the shortest one.


Right, it is incorrect, but that still wouldn't stop some people from jokingly postulating it as a solution. ;)
Very much not unlike:
[ATTACH=CONFIG]24526[/ATTACH]