Quine: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
mNo edit summary
m (Text replacement - "</source>" to "</syntaxhighlight>")
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
A '''[[wikipedia:quine|quine]]''' is a program which takes no input and produces a copy of its own source code as its only output.
A '''[[wikipedia:quine|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":
In APL, a quine is listed in the [[FinnAPL idiom library]] as "an expression giving itself":
<source lang=apl>
<syntaxhighlight lang=apl>
       1⌽22⍴11⍴'''1⌽22⍴11⍴'''
       1⌽22⍴11⍴'''1⌽22⍴11⍴'''
</source>
</syntaxhighlight>
 
=== Modern update ===


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>
<source lang=apl>
<syntaxhighlight lang=apl>
       1⌽,⍨9⍴'''1⌽,⍨9⍴'''
       1⌽,⍨9⍴'''1⌽,⍨9⍴'''
</source>
</syntaxhighlight>


Explanation:<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>
==== Explanation ====
* <source lang=apl inline>'''1⌽,⍨9⍴'''</source> the characters <source lang=text inline>'1⌽,⍨9⍴'</source>
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>
* <source lang=apl inline>9⍴</source> reshape to shape 9, resulting in <source lang=text inline>'1⌽,⍨9⍴''</source>
* <syntaxhighlight lang=apl inline>'''1⌽,⍨9⍴'''</syntaxhighlight> the characters <syntaxhighlight lang=text inline>'1⌽,⍨9⍴'</syntaxhighlight>
* <source lang=apl inline>,⍨</source> concatenate with itself, resulting in <source lang=text inline>'1⌽,⍨9⍴'''1⌽,⍨9⍴''</source>
* <syntaxhighlight lang=apl inline>9⍴</syntaxhighlight> reshape to shape 9, resulting in <syntaxhighlight lang=text inline>'1⌽,⍨9⍴''</syntaxhighlight>
* <source lang=apl inline>1⌽</source> rotate one character to the left, getting the characters: <source lang=text inline>1⌽,⍨9⍴'''1⌽,⍨9⍴'''</source>
* <syntaxhighlight lang=apl inline>,⍨</syntaxhighlight> concatenate with itself, resulting in <syntaxhighlight lang=text inline>'1⌽,⍨9⍴'''1⌽,⍨9⍴''</syntaxhighlight>
* <syntaxhighlight lang=apl inline>1⌽</syntaxhighlight> rotate one character to the left, getting the characters: <syntaxhighlight lang=text inline>1⌽,⍨9⍴'''1⌽,⍨9⍴'''</syntaxhighlight>


== Further Entries ==
== Further Entries ==
=== 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>
<syntaxhighlight lang=apl>
      '{∊⍵⍺⍵⍺,4/⍵}'{∊⍵⍺⍵⍺,4/⍵}''''
</syntaxhighlight>
==== Explanation ====


In 2022, [[APL Farm]] user OsKaR31415 shared the following:
In this code, the main function is <syntaxhighlight lang=apl inline>{∊⍵⍺⍵⍺,4/⍵}</syntaxhighlight> with the right argument <syntaxhighlight lang=apl inline>⍵</syntaxhighlight> being single quote literal (spelled with four quotes, <syntaxhighlight lang=apl inline>''''</syntaxhighlight>, because single quotes delimit APL character literals, and quotes inside such must be doubled), and the left argument <syntaxhighlight lang=apl inline>⍺</syntaxhighlight> being the character vector representation of the function (<syntaxhighlight lang=apl inline>'{∊⍵⍺⍵⍺,4/⍵}'</syntaxhighlight>).
<source lang=apl>
 
      '{∊⍵⍺⍵⍺,4/⍵}'{∊⍵⍺⍵⍺,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 <syntaxhighlight lang=apl inline></syntaxhighlight> (itself in this case), and character <syntaxhighlight lang=apl inline></syntaxhighlight> (here, the quote), it produces the structure <syntaxhighlight lang=apl inline>⍵ ⍺ ⍵ ⍺ ⍵ ⍵ ⍵ ⍵</syntaxhighlight>. A more explicit version would be <syntaxhighlight lang=apl inline>'{⍵,,,,,,,⍵}'{⍵,,,,,,,⍵}''''</syntaxhighlight> which simply concatenates together the required parts. The original quine is very similar, only using the shorter <syntaxhighlight lang=apl inline>4/⍵</syntaxhighlight> to produce the 4 occurrences of <syntaxhighlight lang=apl inline>⍵</syntaxhighlight>, and [[code golf|golfs]] away the spaces, instead opting for [[Enlist]] (<syntaxhighlight lang=apl inline>∊</syntaxhighlight>) to flatten the intermediary [[nested array]].
</source>
 
Explanation :
=== Based on code point of quote ===
In this code, the main function is <source lang=apl inline>{∊⍵⍺⍵⍺,4/⍵}</source>. In this function, the right argument <source lang=apl inline>⍵</source> is the literal <source lang=apl inline>'</source>, and the left argument <source lang=apl inline>⍺</source> is the string containing the representation of the function : <source lang=apl inline>'{∊⍵⍺⍵⍺,4/⍵}'</source>.
In Jun 2022, [[Jay Foad]] shared the following:<ref>Message {{M|61482904}}. [[APL Orchard]]. 2022-06-30.</ref>
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 : <source lang=text inline>[quote] [function] [quote] [function] [quote] [quote] [quote] [quote]</source>. So this is precisely what the function does : given the representation of a function <source lang=apl inline></source> (itself in this case), and a string <source lang=apl inline></source>(the quote here), it reproduce the structure `⍵ ⍺ ⍵ ⍺ ⍵ ⍵ ⍵ ⍵`. Another quine is <source lang=apl inline>'{∊⍵ ⍺ ⍵ ⍺ ⍵ ⍵ ⍵ ⍵}'{∊⍵ ⍺ ⍵ ⍺ ⍵ ⍵ ⍵ ⍵}''''</source>, and the only thing to notice on this one is the use of <source lang=apl inline>∊</source> so the result is not a nested array of strings, but a flat string.
<syntaxhighlight lang=apl>
The quite proposed here is very similar, it only replaces the 4 occurences of <source lang=apl inline>⍵</source> by <source lang=apl inline>4/⍵</source> and deletes the useless spaces for the purpose of golfing.
      {,⍨⍵,⎕UCS 39}'{,⍨⍵,⎕UCS 39}'
</syntaxhighlight>
==== Explanation ====
In this code, the main function is <syntaxhighlight lang=apl inline>{,⍨⍵,⎕UCS 39}</syntaxhighlight> with the argument <syntaxhighlight lang=apl inline>⍵</syntaxhighlight> 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</syntaxhighlight>).
 
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>⍵</syntaxhighlight> (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>,⍨</syntaxhighlight>) to repeat the two parts into the required four.


== External links ==
== External links ==

Revision as of 22:29, 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":

      1⌽22⍴11⍴'''1⌽22⍴11⍴'''

Modern update

In 2019, Nick Nikolov proposed to shorten it using the commute operator:[1]

      1⌽,⍨9⍴'''1⌽,⍨9⍴'''

Explanation

This shorter version is explain as follows:[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

Based on replicating quote

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