Write the shortest self-identifying program (a quine variant)

Write a program that will generate a "true" output iff the input matches the source code of the program, and which generates a "false" output iff the input does not match the source code of the program.

This problem can be described as being related to quines, as the program must be able to somehow compute its own source code in the process.

This is code golf: standard rules apply. Your program must not access any special files, such as the file of its own source code.

Edit: If you so choose, true/false can be replaced with True/False or 1/0.

Example

If the source code of your program is bhiofvewoibh46948732));:/)4, then here is what your program must do:

Input (Stdin)

bhiofvewoibh46948732));:/)4

true

Input

(Anything other than your source code)

Output

false
• Is the true/false output a strong requirement, or are variations (True/False, 1/0) acceptable as well? – Cristian Lupascu Apr 17 '13 at 14:54
• Is it a problem if the program outputs a little more than true/false (if it keeps being unambiguous and ends with true/false) ? – Denys Séguret Apr 19 '13 at 11:31
• – Ilmari Karonen Jan 11 '14 at 5:28
• So you mean a Narcissist program? – PyRulez Jul 15 '17 at 5:57
• Very much related – Deadcode Jan 20 '20 at 7:37

JavaScript : 26

function f(s){return s==f}

I don't know if a JavaScript file really qualifies as a "program".

main=interact$show.(==s++show s);s="main=interact$show.(==s++show s);s="

Note: there is no end-of-line character at the end of the script.

$runhaskell Self.hs < Self.hs True ><>, 68 bytes Fishes love eating fish poop. Now we know they can distinguish theirs from their friends'. 00v 0+1~$^?)0~\;n0\
>:@@:@gi:0(?\:a=?/=?!/$1+ 0n;n*=f$=2~~/

You can try it online !

• This outputs 1 for any prefix of the code as well – Jo King Sep 30 '18 at 3:12
• @JoKing it was worst than just prefixes of the script, it accepted truncated lines too ! I've fixed it, but I'm disappointed it's not as generic as I wanted it to be, I had to check the reached cell at the end of the script to make sure the whole code was matched. It can certainly be improved, but I'm not sure I will bother. – Aaron Oct 3 '18 at 15:09

GolfScript, 11 chars

{".~"+=}.~

Without the =, this code would be a quine that generates its own source code as a string. The = makes it compare this string to its input and output 1 if they match and 0 if they don't. Note that the comparison is exact — in particular, a trailing newline at the end of the input will cause it to fail.

Explanation:

• { } is a code block literal in GolfScript;
• . duplicates this code block, and ~ executes the second copy (leaving the first on the stack);
•  stringifies the code block, and ".~"+ appends .~ to it;
• finally, = compares the resulting string with the input (which is pushed on the stack as a string by the GolfScript interpreter before the program starts) and returns 1 if they match and 0 if they don't.

Perl, Infinity41 38 Characters

Perl, 52 char

Ṿ     Uneval: '“Ṿ;$⁼”' ; Append string: '“Ṿ;$⁼”Ṿ;$⁼' (source code) ⁼ Is the string above equal to the input? 05AB1E, 15 bytes 0"D34çýQ"D34çýQ Modifies the default 0"D34çý"D34çý by adding Q (check for equality with the implicit input) Try it online. Explanation: 0 # Push 0 to the stack # STACK: [0] "D34çýQ" # Push the string 'D34çýQ' to the stack # STACK: [0, 'D34çýIå'] D # Duplicate this string # STACK: [0, 'D34çýIå', 'D34çýIå'] 34ç # Push '"' to the stack # STACK: [0, 'D34çýIå', 'D34çýIå', '"'] ý # Join the stack by this '"' delimiter # STACK: ['0"D34çýIå"D34çýIå'] Q # Check if it's equal to the (implicit) input # (and output the top of the stack implicitly as result) Cool 15 bytes alternative provided by @Grimy: 187745012D27BJQ Try it online. Explanation: 187745012 # Push integer 187745012 # STACK: [187745012] D # Duplicate it # STACK: [187745012, 187745012] 27 # Push integer 27 # STACK: [187745012, 187745012, 27] B # Convert 187745012 to base-27 # STACK: [187745012, "D27BJQ"] J # Join the values on the stack together # STACK: ["187745012D27BJQ"] Q # Check if it's equal to the (implicit) input # (and output the top of the stack implicitly as result) • 187745012D27BJQ is a tie. – Grimmy Jun 13 '19 at 12:21 R, 54 bytes f=function(s)s==paste0("f=function(s)s==", body(f)[3]) Try it online! body gets the body of the function (splitting it a bit, so that body(f)[3] is everything from paste0 onwards). Interestingly, body reformats the code, adding spaces after commas, etc. This is thus a rare case of an R golf answer with a space after a comma. This works because body(f) is an object of type language, and there exists an as.character method for this type. On the other hand, f and args(f) are of type closure, and cannot be converted to character type as far as I can tell. Please don't ask me what the language type is for… • As we've noted before, R's code-reformatting is quite version-dependent. My old R version 3.2.1 let's me use f=function(x)x==paste0('f=',capture.output(f)) successfully for only 46 bytes, but the later version in TIO fills it up with annoying spaces & newlines... – Dominic van Essen Jan 3 at 11:59 • @DominicvanEssen Good find! capture.output works as you would want on my later version of R (3.6.3), but it adds a bytecode at the end. The output is thus a vector of 2 logicals: the first one corresponds to what you want, the second tests equality with the bytecode. I believe this is acceptable: in R, a vector of two logicals is truthy iff the first logical is TRUE (per the definition along the lines of "how would if handle it?). You should post this separately! – Robin Ryder Jan 3 at 15:35 • Thanks very much for checking that! It seems that only specific R versions don't work (annoyingly including the one at TIO) - I now tested version 4 and it works Ok, too. So I've posted a separate version-dependent R answer now. – Dominic van Essen Jan 4 at 13:59 C - 186 176 characters One liner: *a="*a=%c%s%c,b[999],c[999];main(){sprintf(b,a,34,a,34);gets(c);putchar(strcmp(b,c)?'0':'1');}",b[999],c[999];main(){sprintf(b,a,34,a,34);gets(c);putchar(strcmp(b,c)?'0':'1');} With whitespace (note that this breaks the program): *a="*a=%c%s%c,b[999],c[999];main(){sprintf(b,a,34,a,34);gets(c);putchar(strcmp(b,c)?'0':'1');}",b[999],c[999]; main() { sprintf(b,a,34,a,34); gets(c); putchar(strcmp(b,c)?'0':'1'); } Stax, 26 bytes "34bLN26)s:f="34bLN26)s:f= Run and debug it JavaScript ES6, 14 bytes Like the other javascript answer but includes it's entire source code and not just the function a=q=>'a='+a==q Example usage: a("q=>'a='+a==q") // returns false a("a=q=>'a='+a==q") // returns true q, 8 bytes {x~.z.s} Return boolean on input matching the self-referential .z.s Runic, 11 bytes "3X4+kSqi=@ Try it online! TIO got updated and there's no longer an issue reading input (and no longer requires a trailing whitespace). Explanation > Implicit entry " Begin reading as string 3X4+kSqi=@ Pushed to the stack as a string, loop around " End reading as string 3X4+ Push 3*10 and 4 to the stack, add them together k Convert to character (") S Swap the top two items on the stack q Concatenate. This leaves only "3X4+kSqi=@ on the stack i Read input = Compare using .Equals, push 1 if equal, else 0 @ Print and terminate JoKing's solution: "'<~qi=@| Explanation < Entry ' Read character (loop around) " Push " | Mirror " Begin reading string (loop around) '<~ri=@| Push the string '<~qi=@| (loop around) " End reading string '<~ Push the character < and then discard it q Concatenate, stack contains only "'<~qi=@| i Read input = Compare @ Print and terminate • 10 bytes – Jo King Sep 30 '18 at 6:37 • @JoKing Very clever. – Draco18s no longer trusts SE Sep 30 '18 at 15:00 • Actually, 9 bytes avoids the reverse – Jo King Sep 30 '18 at 15:24 • @JoKing I probably should've been able to arrive at that (from the 10 byte solution) myself, but I haven't had my cawfee yet. I'd already worked out yesterday that having the " on the left is the only place it can really go, because having it elsewhere complicates things. (But just now I had to run it in my debugger to see what it was doing...) – Draco18s no longer trusts SE Sep 30 '18 at 15:40 Zsh, 33 bytes f () { [ "type -f f" =$1 ]
}

Try it online!

The extra spaces, tab, and newlines are required. type -f f does not print the original source, but the function formatted in a particular way, with indentation and a trailing newline.