Quine: Difference between revisions
m (→⎕UCS) |
|||
Line 29: | Line 29: | ||
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 <source lang=apl inline>⍺</source> (itself in this case), and character <source lang=apl inline>⍵</source> (here, the quote), it produces the structure <source lang=apl inline>⍵ ⍺ ⍵ ⍺ ⍵ ⍵ ⍵ ⍵</source>. A more explicit version would be <source lang=apl inline>'{⍵,⍺,⍵,⍺,⍵,⍵,⍵,⍵}'{⍵,⍺,⍵,⍺,⍵,⍵,⍵,⍵}''''</source> which simply concatenates together the required parts. The original quine is very similar, only using the shorter <source lang=apl inline>4/⍵</source> to produce the 4 occurrences of <source lang=apl inline>⍵</source>, and [[code golf|golfs]] away the spaces, instead opting for [[Enlist]] (<source lang=apl inline>∊</source>) to flatten the intermediary [[nested array]]. | 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 <source lang=apl inline>⍺</source> (itself in this case), and character <source lang=apl inline>⍵</source> (here, the quote), it produces the structure <source lang=apl inline>⍵ ⍺ ⍵ ⍺ ⍵ ⍵ ⍵ ⍵</source>. A more explicit version would be <source lang=apl inline>'{⍵,⍺,⍵,⍺,⍵,⍵,⍵,⍵}'{⍵,⍺,⍵,⍺,⍵,⍵,⍵,⍵}''''</source> which simply concatenates together the required parts. The original quine is very similar, only using the shorter <source lang=apl inline>4/⍵</source> to produce the 4 occurrences of <source lang=apl inline>⍵</source>, and [[code golf|golfs]] away the spaces, instead opting for [[Enlist]] (<source lang=apl inline>∊</source>) to flatten the intermediary [[nested array]]. | ||
=== <source lang=apl inline> | === <source lang=apl inline>Based on code point of quote</source> === | ||
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> | ||
<source lang=apl> | <source lang=apl> |
Revision as of 10:03, 30 June 2022
A quine is a program which takes no input and produces a copy of its own source code as its only output.
In APL, a quine is listed in the FinnAPL idiom library as "an expression giving itself":
1⌽22⍴11⍴'''1⌽22⍴11⍴'''
In 2019, Nick Nikolov proposed to shorten it using the commute operator:[1]
1⌽,⍨9⍴'''1⌽,⍨9⍴'''
Explanation:[2]
'''1⌽,⍨9⍴'''
the characters'1⌽,⍨9⍴'
9⍴
reshape to shape 9, resulting in'1⌽,⍨9⍴''
,⍨
concatenate with itself, resulting in'1⌽,⍨9⍴'''1⌽,⍨9⍴''
1⌽
rotate one character to the left, getting the characters:1⌽,⍨9⍴'''1⌽,⍨9⍴'''
Further Entries
Replicate
In May 2022, APL Farm user OsKaR31415 shared the following:[3]
'{∊⍵⍺⍵⍺,4/⍵}'{∊⍵⍺⍵⍺,4/⍵}''''
Explanation
In this code, the main function is {∊⍵⍺⍵⍺,4/⍵}
with the right argument ⍵
being single quote literal (spelled with four quotes, ''''
, because single quotes delimit APL character literals, and quotes inside such must be doubled), and the left argument ⍺
being the character vector representation of the function ('{∊⍵⍺⍵⍺,4/⍵}'
).
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 ⍺
(itself in this case), and character ⍵
(here, the quote), it produces the structure ⍵ ⍺ ⍵ ⍺ ⍵ ⍵ ⍵ ⍵
. A more explicit version would be '{⍵,⍺,⍵,⍺,⍵,⍵,⍵,⍵}'{⍵,⍺,⍵,⍺,⍵,⍵,⍵,⍵}''''
which simply concatenates together the required parts. The original quine is very similar, only using the shorter 4/⍵
to produce the 4 occurrences of ⍵
, and golfs away the spaces, instead opting for Enlist (∊
) to flatten the intermediary nested array.
Based on code point of quote
In Jun 2022, Jay Foad shared the following:[4]
{,⍨⍵,⎕UCS 39}'{,⍨⍵,⎕UCS 39}'
Explanation
In this code, the main function is {,⍨⍵,⎕UCS 39}
with the argument ⍵
being the function itself which avoids duplication of quotes by generating the quote from its Universal Character Set code point (⎕UCS
).
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 ⍵
(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 (,⍨
) 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.