Narcissistic Quine

19

2

Write a program/method that:

  • Becomes a quine when the input matches the source code;
  • Doesn't halt otherwise.

This is code-golf, so shortest solution wins. Avoid any standard loopholes.

Dannyu NDos

Posted 2020-01-26T04:34:22.757

Reputation: 2 087

1@PostRockGarfHunter Strengthened the requirements. Now this challenge differs from a cat program. – Dannyu NDos – 2020-01-26T04:54:30.330

8This is why we sandbox. – Post Rock Garf Hunter – 2020-01-26T05:00:35.680

Here's a similar question, hopefully the answers can provide inspiration for others! https://codegolf.stackexchange.com/questions/11370

– Josh – 2020-01-27T21:53:19.000

Answers

5

Jelly, 14 bytes

“Ṿ;⁾v`¹⁻³$¿”v`

Try it online!

A program taking a single string argument and either printing its source if the string matches its source, or looping indefinitely if not.

Explanation

“          ”v` | Evaluate the following string as Jelly code, using the string itself as argument:
 Ṿ             | - Uneval (effectively wrap the string in “”
  ;⁾v`         | - Append v`
       ⁻³$¿    | - While not equal to the original program argument:
      ¹        |   - Call the identity function

Nick Kennedy

Posted 2020-01-26T04:34:22.757

Reputation: 11 829

13

Python 3.8 (pre-release), 67 60 56 bytes

exec(a:='print(s:=input())\nwhile s!="exec(a:=%r)"%a:1')

Try it online!


Python 2, 56 bytes

a='s=input();print s\nwhile s!="a=%r;exec a"%a:1';exec a

Try it online!

Mukundan

Posted 2020-01-26T04:34:22.757

Reputation: 1 188

2Is there anything in 3.8 that this answer relies on? – Cruncher – 2020-01-27T21:24:36.227

3

@Cruncher https://docs.python.org/3/whatsnew/3.8.html#assignment-expressions

– Aaron – 2020-01-27T22:07:01.113

8

GolfScript, 20 bytes

{`".~"+{.2$=}do\;}.~

Try it online!

Explanation

{                }.~ # Evaluate the following string as GolfScript code, using the string itself as argument:
 `                   # - Uneval (effectively wrap the string in ""
  ".~"+              # - Append .~
       {.2$=}do      # - Do While not equal to the original program argument:
                     #   - (Basically do nothing)
               \;    # - If this loop ends, discard the extra copy of the input

user85052

Posted 2020-01-26T04:34:22.757

Reputation:

3What is the relationship between Jelly and GS? – RGS – 2020-01-26T15:20:20.533

1@RGS It's simply because Jelly is (indirectly) based on GS. – None – 2020-01-26T22:52:56.940

1why did the GS port of a jelly answer get more votes than the answer it was porting? – Cruncher – 2020-01-27T21:25:35.250

5

05AB1E, 20 bytes

"34çìD«Êi["34çìD«Êi[

Try it online!


Explanation

"34çìD«Êi["           - string literal
           34ç        - push the character "
              ì       - prepend to the string literal
               D«     - duplicate the string (producing the code)
                 Êi[  - loop if it's not the same as the input
                      - else implicitly output

Expired Data

Posted 2020-01-26T04:34:22.757

Reputation: 3 129

2

PHP, 89 bytes

eval($s='$a=chr(39);$b="eval(\$s=$a";$c="$a);";$b.$s.$c!=$argn?:die($b.$s.$c);for(;;);');

Try it online!

This is the best I have found so far, at the beginning it wasn't a port of @Mukundan's answer, but it pretty much ends like it. I will try to improve it later..

EDIT: saved a byte with != instead of == so I can remove the 1

EDIT2: saved 16 bytes using vars for repeated strings

Kaddath

Posted 2020-01-26T04:34:22.757

Reputation: 449

2

RProgN 2, 17 bytes

«•.x=xe²xw³1#1:? 

Explanation

«•.x=xe²xw³1#1:?<SPACE> #
«                       # Push the following function to the stack and execute
 •.                     # Append a space (A non-function character) and stringify
   x=                   # Assign the stringified function to x
     xe                 # Is x equal to the top of the stack?
       ²                # "Double Function" to execute if true
        x               # Push x to the stack
         w              # Output
          ³             # "Tripple Function" to execute if false
           1            # Push a truthy value to the stack
            #1          # A function that just repeatedly pushes true
              :         # While the top of the stack is true. Thus, loop forever
               ?        # If a, than b, else c
                <SPACE> # Used to ensure the function qualifies as a quine.

Try it online!

ATaco

Posted 2020-01-26T04:34:22.757

Reputation: 7 898

2

Funky, 36 bytes

f=s=>{x=`f=[f]`ifx==s x elsewhile1z}

Try it online!

ATaco

Posted 2020-01-26T04:34:22.757

Reputation: 7 898

2

JavaScript (V8), 33 32 31 bytes

-1 byte thanks to Jo King

f=x=>eval(`while(x!='f='+f);x`)

Try it online!

Expired Data

Posted 2020-01-26T04:34:22.757

Reputation: 3 129

20 bytes, though I'm not sure if exceeding the recursion limit counts as infinite. At the very least, you can get rid of the trailing semicolon – Jo King – 2020-01-30T13:04:12.923

@JoKing Yeah I thought about that but I don't think that really counts, since it's bounded by the size of the stack, thanks for the -1, I did think about something similar with f=x=>x=='f='+f?x:eval("for(;;);") but it's 33 bytes – Expired Data – 2020-01-30T13:22:06.560

Another 33 f=x=>eval(\while(x!='f='+f);`)||x` – Expired Data – 2020-01-30T13:26:47.893