# 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


## Output (Stdout)

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

# 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. # Perl 6, 33 bytes <dd "<$_>~~.EVAL"eq slurp>~~.EVAL


Try it online!

Outputs either Bool::True or Bool::False to STDERR. This is the basic quine format, with an added check against input (eq slurp).

# Keg, 3628 26 bytes

:&\ⁿ^\ⁿ&⅀=:&\ⁿ^\ⁿ&⅀=


Try it online!

Same concept as before, but shorter.

:&\ⁿ⅍^\ⁿ⅍++&+¿=:&\ⁿ⅍^\ⁿ⅍++&+¿=


Try it online!

Who knew generating a program that knows itself would be so unreadable!

## Explained

The very first step here is to generate the string that will be checked against. At this point, we don't particularly know what the checking part will look like, but we do know that there will be a string that looks like so:

\ⁿ⅍^\ⁿ⅍++


Which would consequently be followed by:

 \ⁿ⅍^\ⁿ⅍++


This creates a string which contains itself quoted... Something needed for when input is taken, as the quotes are going to be present.

From here, string equality is quite easy, coming in at a trivial 2 bytes:

¿=


Incorporating this into the string gives:

\ⁿ⅍^\ⁿ⅍++¿=\ⁿ⅍^\ⁿ⅍++¿=


At this stage, one might think the quine is complete... It has the string part and it has the equality part... What else could be needed? Well. Let me tell you we ain't done yet.

Y'see, we have a quoted string on the stack, but we don't have it's unquoted equivalent present, meaning we need to account for that. The register works well here:

:&\ⁿ⅍^\ⁿ⅍++&+¿=:&\ⁿ⅍^\ⁿ⅍++&+¿=


And there you have it: a self identifying program written completely on a phone keyboard.

• It still outputs 1 if the input is followed by a newline and then anything else – Jo King Jan 19 '20 at 23:32
• Well of course it would, because Keg only reads one line at a time. I'll have to clarify input methods with Keg, because otherwise multi line input isn't possible. – lyxal Jan 19 '20 at 23:38

#PowerShell, 28 bytes

Very similar to this JavaScript answer, uses function provider.

filter f{"$Function:f"-eq$_}


#Example

PS > '"$Function:f"-eq$_' | f
True

PS > 'xxx' | f
False


# R (version-dependent), 46 bytes

f=function(x)x==paste0('f=',capture.output(f))


capture.output - as the function name suggests - retrieves the text string that results from entering the function name f directly to R, which should output the body of the function. We manually prepend this with f= so that it exactly* matches the program code, and check whether the function argument x is the same as this.
*See below

There are a couple of caveats, though, that depend on the R version used:

1. Early versions of R (like my own version 3.2.1) return the originally-defined function body as-is when the function name is entered to R. However, later versions add additional formatting, like inserting spaces and newlines, which breaks this code: this unfortunately includes the installation (3.5.2) on 'Try it online'. Later still, much newer R versions (I tested 4.0.3) return to the as-is output formatting and everything is Ok again... except...
2. The most recent versions of R compile functions at the first run, and thereafter append a 'byte code' specific for the compiled function to the outputted function body. This means that capture.output returns two strings, beginning at the second time the function is run. Luckily, the function body is still the first one (the byte code is the second one), so the result of supplying the program code as input is TRUE FALSE, which evaluates to 'truthy' in R and so counts as a 'true' output for this challenge.

Special thanks to Robin Ryder for helping to uncover the version-dependence of R's function outputting.

# Ruby-0n, 32 27 bytes

Saved 5 bytes thanks to a comment by @Sisyphus on another answer.

eval s="p$_=='eval s=%p'%s"  Try it online! Disclosure: very similar to my answer on a duplicate question. The -0 flag sets the null byte as the input record separator. Without -0, each line of the input would be read (and tested for equality with the code) separately. # V, 19 bytes ñOÑ~"qpxØ¥^¨©î±¥$


Try it online!

Prints 1 or 0. Here is a hexdump:

00000000: f14f d11b 7e22 7170 78d8 a55e a881 a9ee  .O..~"qpx..^....
00000010: b1a5 24                                  ..$ # Java 8, 288 bytes (Full Program with STDIN) interface M{static void main(String[]a){String s="interface M{static void main(String[]a){String s=%c%s%1$c;System.out.print(s.format(s,34,s).equals(new java.util.Scanner(System.in).nextLine()));}}";System.out.print(s.format(s,34,s).equals(new java.util.Scanner(System.in).nextLine()));}}


Try it online.

interface M{static void main(String[]a){String s="interface M{static void main(String[]a){String s=%c%s%1$c;System.out.print(s.format(s,34,s).equals(a[0]));}}";System.out.print(s.format(s,34,s).equals(a[0]));}}  Try it online. # Java 8, 108 bytes (Function) i->{String s="i->{String s=%c%s%1$c;return s.format(s,34,s).equals(i);}";return s.format(s,34,s).equals(i);}


Try it online.

Explanation:

-part:

• The String s contains the unformatted source code.
• %s is used to input this String into itself with the s.format(...).
• %c, %1\$c and the 34 are used to format the double-quotes.
• s.format(s,34,s) puts it all together

Challenge-part:

• .equals(...) checks if this formatted source code equals the input.
• java.util.Scanner(System.in).nextLine() is used as this input for STDIN
• a[0] is used as this input for Console arguments
• i is used as this input for the lambda function

# Pascal (FPC), 167 157 bytes

const b=#39';var s:string;begin read(s);write(s=a+b+#39#59#97#61#39+a+b)end.';a='const b=#39';var s:string;begin read(s);write(s=a+b+#39#59#97#61#39+a+b)end.


Try it online!

While substring can be easily extracted, like in my regular quine, unfortunately, they cannot be concatenated because FPC thinks it is an array in these circumstances. In a and b are characters before and after constant definitions. #39#59#97#61#39 is a replacement for ';a=' as this is not represented in the constants. #39 represents ' and is at the start of b, glued together to the rest of b, to shorten the comparison expression as much as possible.

# TeX, 55 bytes

The file has to be saved as a.tex and should be run using pdftex a.tex. The script doesn't terminate after returning 1 or 0, if that should be necessary append it by \end (+4 bytes). If it should work for arbitrary file names replace \openin0a with \openin0\jobname (+7 bytes).

\openin0=a\read0to\0\read1to~\message{\ifx\0~1\else0\fi}


# Python 3.8 (pre-release), 42 bytes

exec(s:="print('exec(s:=%r)'%s==input())")


Try it online!

# Japt, 22 16 14 bytes

"iQ ²¶U"iQ ²¶U


Try it online!

## Explanation

"iQ ²¶U"iQ ²¶U
"iQ ²¶U"        // Take this string.        iQ ²¶U
iQ      // Insert a quote.          "iQ ²¶U
²    // Double.                  "iQ ²¶U"iQ ²¶U
¶U  // Check if equal to input.


# Java, 318 bytes

interface Q{static void main(String[]a){char q=34;String s="interface Q{static void main(String[]a){char q=34;String s=%c%s%c;System.out.print(new java.util.Scanner(System.in).nextLine().equals(String.format(s,q,s,q)));}}";System.out.print(new java.util.Scanner(System.in).nextLine().equals(String.format(s,q,s,q)));}}


Try it online!