Quine: Difference between revisions
No edit summary |
m (Text replacement - "<source" to "<syntaxhighlight") |
||
Line 4: | Line 4: | ||
In APL, a quine is listed in the [[FinnAPL idiom library]] as "an expression giving itself": | In APL, a quine is listed in the [[FinnAPL idiom library]] as "an expression giving itself": | ||
< | <syntaxhighlight lang=apl> | ||
1⌽22⍴11⍴'''1⌽22⍴11⍴''' | 1⌽22⍴11⍴'''1⌽22⍴11⍴''' | ||
</source> | </source> | ||
Line 11: | Line 11: | ||
In 2019, [[Nick Nikolov]] proposed to shorten it using the [[commute]] operator:<ref>[https://chat.stackexchange.com/transcript/message/48380903#48380903 Transcript for 2019-01-06] – [[APL Orchard]].</ref> | In 2019, [[Nick Nikolov]] proposed to shorten it using the [[commute]] operator:<ref>[https://chat.stackexchange.com/transcript/message/48380903#48380903 Transcript for 2019-01-06] – [[APL Orchard]].</ref> | ||
< | <syntaxhighlight lang=apl> | ||
1⌽,⍨9⍴'''1⌽,⍨9⍴''' | 1⌽,⍨9⍴'''1⌽,⍨9⍴''' | ||
</source> | </source> | ||
Line 17: | Line 17: | ||
==== Explanation ==== | ==== Explanation ==== | ||
This shorter version is explain as follows:<ref>[https://codegolf.stackexchange.com/a/178459/87954 Golf you a quine for great good! – APL (Dyalog Unicode), 18 bytes] – Code Golf Stack Exchange.</ref> | This shorter version is explain as follows:<ref>[https://codegolf.stackexchange.com/a/178459/87954 Golf you a quine for great good! – APL (Dyalog Unicode), 18 bytes] – Code Golf Stack Exchange.</ref> | ||
* < | * <syntaxhighlight lang=apl inline>'''1⌽,⍨9⍴'''</source> the characters <syntaxhighlight lang=text inline>'1⌽,⍨9⍴'</source> | ||
* < | * <syntaxhighlight lang=apl inline>9⍴</source> reshape to shape 9, resulting in <syntaxhighlight lang=text inline>'1⌽,⍨9⍴''</source> | ||
* < | * <syntaxhighlight lang=apl inline>,⍨</source> concatenate with itself, resulting in <syntaxhighlight lang=text inline>'1⌽,⍨9⍴'''1⌽,⍨9⍴''</source> | ||
* < | * <syntaxhighlight lang=apl inline>1⌽</source> rotate one character to the left, getting the characters: <syntaxhighlight lang=text inline>1⌽,⍨9⍴'''1⌽,⍨9⍴'''</source> | ||
== Further Entries == | == Further Entries == | ||
=== Based on replicating quote === | === Based on replicating quote === | ||
In May 2022, [[APL Farm]] user OsKaR31415 shared the following:<ref>[https://discord.com/channels/821509511977762827/821511138184396840/976205210580037662 Transcript]. [[APL Farm]]. 2022-05-17.</ref> | In May 2022, [[APL Farm]] user OsKaR31415 shared the following:<ref>[https://discord.com/channels/821509511977762827/821511138184396840/976205210580037662 Transcript]. [[APL Farm]]. 2022-05-17.</ref> | ||
< | <syntaxhighlight lang=apl> | ||
'{∊⍵⍺⍵⍺,4/⍵}'{∊⍵⍺⍵⍺,4/⍵}'''' | '{∊⍵⍺⍵⍺,4/⍵}'{∊⍵⍺⍵⍺,4/⍵}'''' | ||
</source> | </source> | ||
==== Explanation ==== | ==== Explanation ==== | ||
In this code, the main function is < | In this code, the main function is <syntaxhighlight lang=apl inline>{∊⍵⍺⍵⍺,4/⍵}</source> with the right argument <syntaxhighlight lang=apl inline>⍵</source> being single quote literal (spelled with four quotes, <syntaxhighlight lang=apl inline>''''</source>, because single quotes delimit APL character literals, and quotes inside such must be doubled), and the left argument <syntaxhighlight lang=apl inline>⍺</source> being the character vector representation of the function (<syntaxhighlight lang=apl inline>'{∊⍵⍺⍵⍺,4/⍵}'</source>). | ||
The idea behind this quine is that the only thing you need to have a string containing the original code is to reproduce this structure: [quote] [function] [quote] [function] [quote] [quote] [quote] [quote]. This is precisely what the function does. Given the representation of a function < | The idea behind this quine is that the only thing you need to have a string containing the original code is to reproduce this structure: [quote] [function] [quote] [function] [quote] [quote] [quote] [quote]. This is precisely what the function does. Given the representation of a function <syntaxhighlight lang=apl inline>⍺</source> (itself in this case), and character <syntaxhighlight lang=apl inline>⍵</source> (here, the quote), it produces the structure <syntaxhighlight lang=apl inline>⍵ ⍺ ⍵ ⍺ ⍵ ⍵ ⍵ ⍵</source>. A more explicit version would be <syntaxhighlight lang=apl inline>'{⍵,⍺,⍵,⍺,⍵,⍵,⍵,⍵}'{⍵,⍺,⍵,⍺,⍵,⍵,⍵,⍵}''''</source> which simply concatenates together the required parts. The original quine is very similar, only using the shorter <syntaxhighlight lang=apl inline>4/⍵</source> to produce the 4 occurrences of <syntaxhighlight lang=apl inline>⍵</source>, and [[code golf|golfs]] away the spaces, instead opting for [[Enlist]] (<syntaxhighlight lang=apl inline>∊</source>) to flatten the intermediary [[nested array]]. | ||
=== Based on code point of quote === | === Based on code point of quote === | ||
In Jun 2022, [[Jay Foad]] shared the following:<ref>Message {{M|61482904}}. [[APL Orchard]]. 2022-06-30.</ref> | In Jun 2022, [[Jay Foad]] shared the following:<ref>Message {{M|61482904}}. [[APL Orchard]]. 2022-06-30.</ref> | ||
< | <syntaxhighlight lang=apl> | ||
{,⍨⍵,⎕UCS 39}'{,⍨⍵,⎕UCS 39}' | {,⍨⍵,⎕UCS 39}'{,⍨⍵,⎕UCS 39}' | ||
</source> | </source> | ||
==== Explanation ==== | ==== Explanation ==== | ||
In this code, the main function is < | In this code, the main function is <syntaxhighlight lang=apl inline>{,⍨⍵,⎕UCS 39}</source> with the argument <syntaxhighlight lang=apl inline>⍵</source> being the function itself which avoids duplication of quotes by generating the quote from its [[Universal Character Set]] code point (<syntaxhighlight lang=apl inline>⎕UCS</source>). | ||
The idea behind this quine is that the only thing you need to have a string containing the original code is to reproduce this structure: [function] [quote] [function] [quote]. This is precisely what the function does. Given the representation of a function < | The idea behind this quine is that the only thing you need to have a string containing the original code is to reproduce this structure: [function] [quote] [function] [quote]. This is precisely what the function does. Given the representation of a function <syntaxhighlight lang=apl inline>⍵</source> (itself in this case), it generates a quote and concatenates it on the right of the function definition, giving [function] [quote]. It then uses [[commute|self]]-[[catenate|concatenation]] (<syntaxhighlight lang=apl inline>,⍨</source>) to repeat the two parts into the required four. | ||
== External links == | == External links == |
Revision as of 22:16, 10 September 2022
A quine is a program which takes no input and produces a copy of its own source code as its only output.
Traditional expression
In APL, a quine is listed in the FinnAPL idiom library as "an expression giving itself": <syntaxhighlight lang=apl>
1⌽22⍴11⍴1⌽22⍴11⍴
</source>
Modern update
In 2019, Nick Nikolov proposed to shorten it using the commute operator:[1] <syntaxhighlight lang=apl>
1⌽,⍨9⍴1⌽,⍨9⍴
</source>
Explanation
This shorter version is explain as follows:[2]
- <syntaxhighlight lang=apl inline>1⌽,⍨9⍴</source> the characters <syntaxhighlight lang=text inline>'1⌽,⍨9⍴'</source>
- <syntaxhighlight lang=apl inline>9⍴</source> reshape to shape 9, resulting in <syntaxhighlight lang=text inline>'1⌽,⍨9⍴</source>
- <syntaxhighlight lang=apl inline>,⍨</source> concatenate with itself, resulting in <syntaxhighlight lang=text inline>'1⌽,⍨9⍴'1⌽,⍨9⍴</source>
- <syntaxhighlight lang=apl inline>1⌽</source> rotate one character to the left, getting the characters: <syntaxhighlight lang=text inline>1⌽,⍨9⍴1⌽,⍨9⍴</source>
Further Entries
Based on replicating quote
In May 2022, APL Farm user OsKaR31415 shared the following:[3] <syntaxhighlight lang=apl>
'{∊⍵⍺⍵⍺,4/⍵}'{∊⍵⍺⍵⍺,4/⍵}'
</source>
Explanation
In this code, the main function is <syntaxhighlight lang=apl inline>{∊⍵⍺⍵⍺,4/⍵}</source> with the right argument <syntaxhighlight lang=apl inline>⍵</source> being single quote literal (spelled with four quotes, <syntaxhighlight lang=apl inline>'</source>, because single quotes delimit APL character literals, and quotes inside such must be doubled), and the left argument <syntaxhighlight lang=apl inline>⍺</source> being the character vector representation of the function (<syntaxhighlight lang=apl inline>'{∊⍵⍺⍵⍺,4/⍵}'</source>).
The idea behind this quine is that the only thing you need to have a string containing the original code is to reproduce this structure: [quote] [function] [quote] [function] [quote] [quote] [quote] [quote]. This is precisely what the function does. Given the representation of a function <syntaxhighlight lang=apl inline>⍺</source> (itself in this case), and character <syntaxhighlight lang=apl inline>⍵</source> (here, the quote), it produces the structure <syntaxhighlight lang=apl inline>⍵ ⍺ ⍵ ⍺ ⍵ ⍵ ⍵ ⍵</source>. A more explicit version would be <syntaxhighlight lang=apl inline>'{⍵,⍺,⍵,⍺,⍵,⍵,⍵,⍵}'{⍵,⍺,⍵,⍺,⍵,⍵,⍵,⍵}'</source> which simply concatenates together the required parts. The original quine is very similar, only using the shorter <syntaxhighlight lang=apl inline>4/⍵</source> to produce the 4 occurrences of <syntaxhighlight lang=apl inline>⍵</source>, and golfs away the spaces, instead opting for Enlist (<syntaxhighlight lang=apl inline>∊</source>) to flatten the intermediary nested array.
Based on code point of quote
In Jun 2022, Jay Foad shared the following:[4] <syntaxhighlight lang=apl>
{,⍨⍵,⎕UCS 39}'{,⍨⍵,⎕UCS 39}'
</source>
Explanation
In this code, the main function is <syntaxhighlight lang=apl inline>{,⍨⍵,⎕UCS 39}</source> with the argument <syntaxhighlight lang=apl inline>⍵</source> being the function itself which avoids duplication of quotes by generating the quote from its Universal Character Set code point (<syntaxhighlight lang=apl inline>⎕UCS</source>).
The idea behind this quine is that the only thing you need to have a string containing the original code is to reproduce this structure: [function] [quote] [function] [quote]. This is precisely what the function does. Given the representation of a function <syntaxhighlight lang=apl inline>⍵</source> (itself in this case), it generates a quote and concatenates it on the right of the function definition, giving [function] [quote]. It then uses self-concatenation (<syntaxhighlight lang=apl inline>,⍨</source>) to repeat the two parts into the required four.
External links
- ↑ Transcript for 2019-01-06 – APL Orchard.
- ↑ Golf you a quine for great good! – APL (Dyalog Unicode), 18 bytes – Code Golf Stack Exchange.
- ↑ Transcript. APL Farm. 2022-05-17.
- ↑ Message 61482904. APL Orchard. 2022-06-30.