![]() |
|
Navigation |
Synopsis A self-reproducing program.
Description A quine
![]() At The Quine Page ![]() Learning about quines, is about learning how to quote and escape symbols in strings.
Examples
module demo::basic::Quine import IO; import String; public void quine(){ println(program);If you look at the source code, ![]() program has as value
the text of the module Quine upto ![]() program ends at ![]() The function quine prints the string program twice:
" ) and backslash (\ ) in strings.
Let's do a simple experiment: And indeed, the two quotes are now properly escaped. This is exactly what happens at ![]() quine :
println("\"" + escape(program, ("\"" : "\\\"", "\\" : "\\\\")) + "\";");We escape program and replace " by \" , and \ by \\ .
The mesmerizing amount of \ characters can be explained due to escaping " and \ .
Now let's put quine to the test.
rascal>import demo::basic::Quine; ok rascal>quine(); module demo::basic::Quine import IO; import String; public void quine(){ println(program); println("\"" + escape(program, ("\"" : "\\\"", "\\" : "\\\\")) + "\";"); } str program = "module demo::basic::Quine import IO; import String; public void quine(){ println(program); println(\"\\\"\" + escape(program, (\"\\\"\" : \"\\\\\\\"\", \"\\\\\" : \"\\\\\\\\\")) + \"\\\";\"); } str program ="; okIf you follow this output line-by-line you will see that it is identical to the original source code of module Quine .
![]() |