| Bytes | Lang | Time | Link |
|---|---|---|---|
| 081 | AutoHotkey v2 | 241212T091831Z | Xuesong |
| 038 | Uiua | 230930T123831Z | lyxal |
| 475 | Vyxal | 230927T114219Z | lyxal |
| 011 | Japt | 230605T233311Z | Shaggy |
| 132 | C gcc | 230603T040413Z | landfill |
| 048 | Julia 1.0 | 230603T133958Z | MarcMush |
| 012 | Vyxal | 210519T204902Z | Aaroneou |
| 027 | Ruby 0n | 201222T232410Z | Dingus |
| 318 | Java | 210225T020543Z | Unmitiga |
| 046 | R versiondependent | 210104T125201Z | Dominic |
| 011 | GolfScript | 140111T053802Z | Ilmari K |
| 028 | #PowerShell | 160112T142157Z | beatcrac |
| 014 | Japt | 200629T075756Z | Mukundan |
| 042 | Python 3.8 prerelease | 200128T062120Z | Mukundan |
| 026 | Keg | 200119T233020Z | lyxal |
| 041 | Smalltalk Pharo 2.0 dialect | 130420T012324Z | aka.nice |
| 033 | Perl 6 | 200119T234730Z | Jo King |
| 055 | TeX | 190909T135546Z | Skillmon |
| 054 | R | 190909T131018Z | Robin Ry |
| 033 | Zsh | 190909T102714Z | GammaFun |
| 015 | 05AB1E | 180907T134117Z | Kevin Cr |
| 068 | ><> | 160112T141004Z | Aaron |
| 157 | Pascal FPC | 181002T151552Z | AlexRace |
| 011 | Runic | 180930T030437Z | Draco18s |
| 008 | q | 180930T021852Z | Thaufeki |
| 010 | Jelly | 180502T185231Z | dylnan |
| 014 | JavaScript ES6 | 180502T181103Z | pfg |
| 026 | Stax | 180327T043248Z | Weijun Z |
| 024 | ><> | 180323T032000Z | Jo King |
| 040 | Python 2 | 180322T220906Z | totallyh |
| 011 | Husk | 180129T150047Z | ბიმო |
| 288 | Java 8 | 180129T125000Z | Kevin Cr |
| 187 | Python | 150919T003657Z | Emilio |
| 019 | V | 170714T200742Z | DJMcMayh |
| 012 | CJam | 170714T195954Z | Business |
| 047 | Python 2 | 160514T151442Z | Riker |
| 014 | JavaScript ES6 | 160404T222618Z | Conor O& |
| 176 | C | 140111T011729Z | Josh |
| 026 | GolfScript | 140110T203951Z | aditsu q |
| 038 | Perl | 130419T092216Z | user7486 |
| 312 | flex | 130420T051850Z | Geoff Re |
| 026 | JavaScript | 130417T171702Z | Denys S& |
| 054 | Node.js | 130417T174058Z | Denys S& |
| 052 | Perl | 130417T193527Z | mob |
| 035 | JavaScript V8 | 130418T121353Z | Griffin |
| 072 | Haskell | 130418T073340Z | hammar |
| 133 | D | 130418T005949Z | ratchet |
| 111 | Tcl | 130417T151848Z | Johannes |
| 055 | Python 2 | 130417T151448Z | flornqua |
AutoHotkey (v2), input as arguments, output to MsgBox, 91 93 87 81 bytes
Case insensitive comparison. It's OK since function & var names and strings are all case insensitive
Let it be as strict as possible. Change to a case sensitive comparison.
MsgBox Format(s:='MsgBox Format(s:={:c}{}{1:c},39,s)==A_Args[1]',39,s)==A_Args[1]
It takes command line arguments as input (which need to be double quoted) and outputs in MsgBox.
AutoHotkey (v2), input from stdin1, output to MsgBox, 194 167 122 bytes
MsgBox Format(s:="MsgBox Format(s:={:c}{}{1:c},34,s,10)==FileOpen('*','4').Read(){3:c}",34,s,10)==FileOpen('*','4').Read()
It turns out that when reading from stdin, AHK will add extra new lines, and it has to be removed with . Improved this by open TrimSTDIN with mode 4, which converts CR LF to LF. Then append LF to the comparing string.
AutoHotkey (v2), input from stdin, output to stdout1, 212 183 138 bytes
FileAppend Format(s:="FileAppend Format(s:={:c}{}{1:c},34,s,10)==FileOpen('*','4').Read(),'*'{3:c}",34,s,10)==FileOpen('*','4').Read(),'*'
1: AutoHotkey scripts usually run as GUI app on Windows, so stdin/stdout need to be "piped" to/from the program. Also, to literally interpret all quotes as-is, single-quoted "here-strings" must be used as input:
echo @'
input 'string' with "quotes" """ `` '
'@ | autohotkey.exe script.ahk | echo
Uiua, 38 bytes
≅&sc⊂∶!(⊂⊂↷.+1@!)."≅&sc⊂:!(⊂⊂↷.+1@!)."
Explained
≅&sc⊂∶!(⊂⊂↷.+1@!)."≅&sc⊂:!(⊂⊂↷.+1@!)."
"≅&sc⊂:!(⊂⊂↷.+1@!)." # The string "≅&sc⊂:!(⊂⊂↷.+1@!)."
. # Duplicate it
!( ) # Call the following function on the top copy:
.+1@! # Push two "
⊂↷ # Append the first
⊂ # Prepend the second
# The top of the stack is now a quotified string
⊂∶ # Prepend the original string without quotes
≅&sc # Does that equal the input?
💎
Created with the help of Luminespire.
Vyxal, 38 bitsv2, 4.75 bytes
`I=`I=
Explained
`I=`I=
= # Does the input equal
`I=` # The string "I="
I # With itself wrapped in backticks prepended
💎
Created with the help of Luminespire.
Japt, 11 bytes
¶BîQi"¶BîQi
¶BîQi"¶BîQi :Implicit input of string
¶ :Is strictly equal to
B : 11
î : Mold to length B
Q : Quotation mark
i : Prepend
"¶BîQi : Literal string
C (gcc), 132 bytes
#define Q(X)main(c,v)int**v;{return!strcmp(#X"\nQ("#X")",v[1]);}
Q(#define Q(X)main(c,v)int**v;{return!strcmp(#X"\nQ("#X")",v[1]);})
It turns out using a macro works out better than using (a)sprintf, even with %c%s%1$c.
I'm not sure if this type of input (a single multiline command-line argument) and output (return value of 1 for true or 0 for false) are allowed.
If they aren't:
C (gcc), 152 bytes
x,y[99];main(z){asprintf(&x,z="x,y[99];main(z){asprintf(&x,z=%c%s%1$c,34,z);gets(y);putchar(48+!strcmp(x,y));}",34,z);gets(y);putchar(48+!strcmp(x,y));}
Assumes an ASCII environment. Reads from stdin, writes 1 or 0 to stdout. Abuses ints as pointers, leaks memory, and uses the dreaded gets :)
In case you're curious, here's the opposite combinations of quine and IO methods:
C (gcc), 136 bytes
x;main(y,v)int**v;{asprintf(&x,y="x;main(y,v)int**v;{asprintf(&x,y=%c%s%1$c,34,y);return!strcmp(x,v[1]);}",34,y);return!strcmp(x,v[1]);}
C (gcc), 158 bytes
#define Q(X)v[40];main(){read(0,v,159);putchar(48+!strcmp(#X"\nQ("#X")",v));}
Q(#define Q(X)v[40];main(){read(0,v,159);putchar(48+!strcmp(#X"\nQ("#X")",v));})
Vyxal, 12 bytes
`:q$+=`:q$+=
I finished making this, and then realized that it is almost identical to the Vyxal quine that @Lyxal wrote, but with the addition of = to check if the input is equal.
Explanation:
# Implicit input
`:q$+=` # Push the string ':q$+='
: # Duplicate the string
q # Quotify; put backticks around the topmost string
$ # Swap the top two values on the stack
+ # Concatenate the two strings
= # Check if the input is equal to the string
# Implicit output
Ruby -0n, 32 27 bytes
Saved 5 bytes thanks to a comment by @Sisyphus on another answer.
eval s="p$_=='eval s=%p'%s"
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.
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)));}}
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:
- 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...
- 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.outputreturns 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 isTRUE 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.
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 returns1if they match and0if they don't.
#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
Japt, 22 16 14 bytes
"iQ ²¶U"iQ ²¶U
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.
Keg, 36 28 26 bytes
`:&\`ⁿ^\`ⁿ&⅀=`:&\`ⁿ^\`ⁿ&⅀=
Same concept as before, but shorter.
Answer History
`:&\`ⁿ⅍^\`ⁿ⅍++&+¿=`:&\`ⁿ⅍^\`ⁿ⅍++&+¿=
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.
Smalltalk (Pharo 2.0 dialect), 41 bytes
Implement this 41 chars method in String (ugly formatting for code-golf):
isItMe^self=thisContext method sourceCode
Then evaluate this in a Workspace (printIt the traditional Smalltalk way)
The input is not read from stdin, it's just a String to which we send the message (what else a program could be in Smalltalk?):
'isItMe^self=thisContext method sourceCode' isItMe.
But we are cheating, sourceCode reads some source file...
Here is a variant with 51 chars which does not:
isItMe
^ self = thisContext method decompileString
And test with:
'isItMe
^ self = thisContext method decompileString' isItMe
If a String in a Workspace is not considered a valid input, then let's see how to use some Dialog Box in 116 chars
Just evaluate this sentence:
(UIManager default request: 'type me') = (thisContext method decompileString withSeparatorsCompacted allButFirst: 7)
Since decompile format includes CR and TAB, we change that withSeparatorsCompacted.
Then we skip the first 7 chars are 'doIt ^ '
Finally a 105 chars variant using stdin, just interpret this sentence from command line, just to feel more mainstream:
Pharo -headless Pharo-2.0.image eval "FileStream stdin nextLine = (thisContext method decompileString withSeparatorsCompacted allButFirst: 7)"
Perl 6, 33 bytes
<dd "<$_>~~.EVAL"eq slurp>~~.EVAL
Outputs either Bool::True or Bool::False to STDERR. This is the basic quine format, with an added check against input (eq slurp).
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}
R, 54 bytes
f=function(s)s==paste0("f=function(s)s==", body(f)[3])
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…
Zsh, 33 bytes
f () {
[ "`type -f f`" = $1 ]
}
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.
05AB1E, 15 bytes
0"D34çýQ"D34çýQ
Modifies the default quine 0"D34çý"D34çý by adding Q (check for equality with the implicit input)
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
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)
><>, 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 !
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.
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.
Runic, 11 bytes
"3X4+kSqi=@
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
Jelly, 10 bytes
“Ṿ;$⁼”Ṿ;$⁼
“Ṿ;$⁼”Ṿ;$⁼
“Ṿ;$⁼” String literal: 'Ṿ;$⁼'
$ Next two links act on the string literal
Ṿ Uneval: '“Ṿ;$⁼”'
; Append string: '“Ṿ;$⁼”Ṿ;$⁼' (source code)
⁼ Is the string above equal to the input?
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
><>, 24 bytes
'1rd3*i={*}50l3-?.~i)*n;
Wrapping string literal followed by checking whether the input is identical to the stack, with a final check that there is no more input.
Husk, 11 bytes
=hS+s"=hS+s
Explanation
The explanation uses ¨ to delimit strings (to avoid unreadable escaping):
"=hS+s -- string literal: ¨=hS+s¨
S+ -- join itself with
s -- | itself "showed": ¨"=hS+s"¨
-- : ¨=hS+s"=hS+s"¨
h -- init: ¨=hS+s"=hS+s¨
= -- is the input equal?
By removing the function = you can verify that it will indeed only match the source itself.
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()));}}
Java 8, 210 bytes (Full Program with Console Arguments)
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]));}}
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);}
Explanation:
quine-part:
- The
String scontains the unformatted source code. %sis used to input this String into itself with thes.format(...).%c,%1$cand the34are 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 STDINa[0]is used as this input for Console argumentsiis used as this input for the lambda function
Python, 187 bytes
import sys;code="import sys;code=!X!;print(sys.stdin.read()==code.replace(chr(33),chr(34)).replace(!X!,code,1))";print(sys.stdin.read()==code.replace(chr(33),chr(34)).replace("X",code,1))
Careful not to add newline at the end. Someone with better Python-fu might be able to shorten it.
V, 19 bytes
ñOÑ~"qpxØ¥^¨©î±¥$
Prints 1 or 0. Here is a hexdump:
00000000: f14f d11b 7e22 7170 78d8 a55e a881 a9ee .O..~"qpx..^....
00000010: b1a5 24 ..$
CJam, 12 bytes
{s"_~"+q=}_~
Explanation
This just uses the standard CJam quine framework.
{s"_~"+q=} e# Push this block (function literal).
_~ e# Copy and run it.
What the block does:
s e# Stringify the top element (this block itself).
"_~"+ e# Append "_~". Now the source code is on the stack.
q e# Read the input.
= e# Check if it equals the source code.
Python 2, 47 bytes
_='_=%r;print _%%_==input()';print _%_==input()
A simple quine with the added check.
JavaScript ES6, 16 14 bytes
$=_=>_==`$=`+$
Minus two bytes thanks to Neil.
31 bytes if we must take input via prompt.
$=_=>prompt()==`$=${$};$()`;$()
38 bytes if we must output via alert.
$=_=>alert(prompt()==`$=${$};$()`);$()
This is the proper way to do it, as Optimizer's answer does not accept the entire source code.
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');
}
GolfScript - 26
":@;[34]@+2*=":@;[34]@+2*=
Inspired from http://esolangs.org/wiki/GolfScript#Examples
Another version:
"[34].@@;+2*="[34].@@;+2*=
Too bad that \ is both swap and escape...
Perl, Infinity 41 38 Characters
$_=q(print<>eq"\$_=q($_);eval"|0);eval
Update: The program no longer ends with a newline, which means it will work correctly on multi-line files. You have to enter input from STDIN without hitting enter. On Windows I was only able to do this by reading from a file.
Original solution:
print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(...
flex - 312 chars
Q \"
N \n
S " "
B \\
P "Q{S}{B}{Q}{N}N{S}{B}n{N}S{S}{Q}{S}{Q}{N}B{S}{B}{B}{N}P{S}{Q}{P}{Q}{N}M{S}{Q}{M}{Q}{N}%%{N}{P}{N}{M}{N} putchar('1');"
M "(.|{N})* putchar('0');"
%%
Q{S}{B}{Q}{N}N{S}{B}n{N}S{S}{Q}{S}{Q}{N}B{S}{B}{B}{N}P{S}{Q}{P}{Q}{N}M{S}{Q}{M}{Q}{N}%%{N}{P}{N}{M}{N} putchar('1');
(.|{N})* putchar('0');
Can probably be made shorter, but it works with multi-line input (necessary since the source code is multiple lines) and even for inputs that contain the program as a substring. It seems many of the answers so far fail on one or both of these.
Compile command: flex id.l && gcc -lfl lex.yy.c
JavaScript : 26
function f(s){return s==f}
I don't know if a JavaScript file really qualifies as a "program".
Node.js : 54
function f(){console.log(f+'f()'==process.argv[2])}f()
You test it by saving it into a file f.js (the exact name has no importance) and using
node f.js "test"
(which outputs false) or
node f.js "$(< f.js)"
(which outputs true)
I also made a different version based on eval :
eval(f="console.log('eval(f='+JSON.stringify(f)+')'==process.argv[2])")
It's now at 72 chars, I'll try to shorten that when I have time.
Perl, 52 char
$_='$/=$\;$_="\$_=\47$_\47;eval";print<>eq$_|0';eval
JavaScript (V8), 35
function i(){alert(prompt()==i+[])}
call i() and it will prompt for input
Haskell, 72 characters
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
D (133 chars)
enum c=q{import std.stdio;import std.algorithm;void main(){auto i=readln();writeln(equal("auto c=q{"~c~"};mixin(c);",i));}};mixin(c);
Tcl, 111 chars
set c {set c {$c};puts [expr {[read stdin] eq [subst -noc \$c]}]};puts [expr {[read stdin] eq [subst -noc $c]}]
Python 2, 55
a='a=%r;print a%%a==raw_input()';print a%a==raw_input()
Tested:
a='a=%r;print a%%a==raw_input()';print a%a==raw_input() -> True
(anything else) -> False