From Rosetta Code
You are encouraged to solve this task according to the task description, using any language you may know.

Quoting from the Esolangs wiki page:

A narcissist (or Narcissus program) is the decision-problem version of a quine.

A quine, when run, takes no input, but produces a copy of its own source code at its output. In contrast, a narcissist reads a string of symbols from its input, and produces no output except a "1" or "accept" if that string matches its own source code, or a "0" or "reject" if it does not.

For concreteness, in this task we shall assume that symbol = character. The narcissist should be able to cope with any finite input, whatever its length. Any form of output is allowed, as long as the program always halts, and "accept", "reject" and "not yet finished" are distinguishable.


Works with: Ada 2005

Took code from Quine, has to be in one line (could be done pretty printed, too, but not as simple).

with Ada.Text_IO;procedure Self is Q:Character:='"';A:String:="with Ada.Text_IO;procedure Self is Q:Character:='';A:String:=;B:String:=A(1..49)&Q&A(50..61)&Q&A&Q&A(62..A'Last);C:String:=Ada.Text_IO.Get_Line;begin Ada.Text_IO.Put_Line(Boolean'Image(B=C));end Self;";B:String:=A(1..49)&Q&A(50..61)&Q&A&Q&A(62..A'Last);C:String:=Ada.Text_IO.Get_Line;begin Ada.Text_IO.Put_Line(Boolean'Image(B=C));end Self;

ALGOL 68[edit]

Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny

Output: T or F depending on input.


Works with: AutoHotkey 1.1
Narcissist(Input) {
FileRead, Source, % A_ScriptFullPath
return Input == Source ? "accept" : "reject"

Example Use:

MsgBox, % Narcissist(FileOpen(A_ScriptFullPath, "r").Read()) "`n"
. Narcissist("This isn't the text you're looking for.")


Prints '1' on success and '0' on failure.

INPUT a$:PRINT -(a$=$(PAGE+34)+$(PAGE+33)):REM INPUT a$:PRINT -(a$=$(PAGE+34)+$(PAGE+33)):REM


Reads from stdin up to the first carriage return, line feed, or EOF. However, the latter is not guaranteed to work on all Befunge implementations - in particular not on Befunge-98 - so a line break is recommended.

Outputs 1 if the given line of input matches the source code, and 0 if it doesn't.



Based upon the quine. Reads until EOF or newline from stdin, and writes "1" or "0" to stdout.

extern void*stdin;main(){ char*p = "extern void*stdin;main(){ char*p = %c%s%c,a[300],b[300];sprintf(a,p,34,p,34);fgets(b,300,stdin);putchar(48+!strcmp(a,b)); }",a[300],b[300];sprintf(a,p,34,p,34);fgets(b,300,stdin);putchar(48+!strcmp(a,b)); }


using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace Narcisisst
class Program
public static void Main(string[] args)
const string path = @"E:\Narcisisst";
string[] thisFile = Directory.GetFiles(path , "Program.cs");
StringBuilder sb = new StringBuilder();
foreach (string readLine in File.ReadLines(thisFile[0]))
string input =String.Empty;
input = Console.ReadLine();

Common Lisp[edit]

Only checks the first line of stdin:


Déjà Vu[edit]

!. = !prompt "Enter my code: " concat( swap !decode!utf-8 !encode!quoted dup swap ) "!. = !prompt \qEnter my code: \q concat( swap !decode!utf-8 !encode!quoted dup swap ) "


: narcissist  [ source ] sliteral compare 0= ;


This version reads until EOF and expects a newline at the end of the input. If this is being checked from a file, make sure that the file has exactly one newline at the end of it.

package main; import "os"; import "fmt"; import "bytes"; import "io/ioutil"; func main() {ios := "os"; ifmt := "fmt"; ibytes := "bytes"; iioutil := "io/ioutil"; zero := "Reject"; one := "Accept"; x := "package main; import %q; import %q; import %q; import %q; func main() {ios := %q; ifmt := %q; ibytes := %q; iioutil := %q; zero := %q; one := %q; x := %q; s := fmt.Sprintf(x, ios, ifmt, ibytes, iioutil, ios, ifmt, ibytes, iioutil, zero, one, x); in, _ := ioutil.ReadAll(os.Stdin); if bytes.Equal(in, []byte(s)) {fmt.Println(one);} else {fmt.Println(zero);};}\n"; s := fmt.Sprintf(x, ios, ifmt, ibytes, iioutil, ios, ifmt, ibytes, iioutil, zero, one, x); in, _ := ioutil.ReadAll(os.Stdin); if bytes.Equal(in, []byte(s)) {fmt.Println(one);} else {fmt.Println(zero);};}

A version respecting the 80 character line limit:

package main
import (
func main() {
s := fmt.Sprintf("%s%c%s%c\n", x, 0x60, x, 0x60)
in, _ := ioutil.ReadAll(os.Stdin)
if bytes.Equal(in, []byte(s)) {
} else {
var x = `package main
import (
func main() {
s := fmt.Sprintf("%s%c%s%c\n", x, 0x60, x, 0x60)
in, _ := ioutil.ReadAll(os.Stdin)
if bytes.Equal(in, []byte(s)) {
} else {
var x = `


main = let fi t e c = if c then t else e in do ct <- getContents; putStrLn $ fi ['a','c','c','e','p','t'] ['r','e','j','e','c','t'] $ take (length ct - 1) ct == let q s = (s ++ show s) in q "main = let fi t e c = if c then t else e in do ct <- getContents; putStrLn $ fi ['a','c','c','e','p','t'] ['r','e','j','e','c','t'] $ take (length ct - 1) ct == let q s = (s ++ show s) in q "

Icon and Unicon[edit]

Translation of: Go

Since we can't put a link statement and program on a single line, we implement a simplified inline sprintf so we don't have to deal with all the double quotes or substrings and offsets. If we'd needed to write multiple procedures on a line it can be done Semi-colons in the language intro

procedure main();yes:="Accept";no:="Reject";pat:="procedure main();yes:=$;no:=$;pat:=$;a:=[yes,no,pat];narc:=char(0)[0:0];pat?{while narc||:=tab(find(char(36))) do{narc||:=image(get(a));move(1)};narc||:=tab(0)};write(if read()==narc then yes else no);end";a:=[yes,no,pat];narc:=char(0)[0:0];pat?{while narc||:=tab(find(char(36))) do{narc||:=image(get(a));move(1)};narc||:=tab(0)};write(if read()==narc then yes else no);end
./narcissist.exe < narcissist.icn

Actually, this version recognizes all files where the first line is the Narcissist.


main=:3 : 0
self=: '#!/j602/bin/jconsole',LF,'main=:',(5!:5<'main'),LF,'main''''',LF
echo self -: stdin''

Example use:

$ ./narcissist.ijs <narcissist.ijs

Note that this assumes a suitable os command line.

Alternative solution:


Example use:

   (-:,~,2#{:)&'(-:,~,2#{:)&''' '(-:,~,2#{:)'
(-:,~,2#{:)&'(-:,~,2#{:)&''' '(-:,~,2#{:)&''(-:,~,2#{:)&'''''''


Works with: SpiderMonkey version 1.7.0

Based upon one of the quines. Outputs 'true' if source is equal to inputted line (newline terminated), 'false' otherwise.

var code='var q=String.fromCharCode(39);print("var code=" + q + code + q + "; eval(code)" == readline())'; eval(code)
Works with: JScript
var oFSO = new ActiveXObject("Scripting.FileSystemObject");
function readfile(fname) {
var h = oFSO.OpenTextFile(fname, 1, false);
var result = h.ReadAll();
return result;
if (0 === WScript.Arguments.UnNamed.Count) {
// first read self
var self = readfile(WScript.ScriptFullName);
// read whatever file is given on commmand line
var whatever = readfile(WScript.Arguments.UnNamed(0));
// compare and contrast
WScript.Echo(self === whatever ? "Accept" : "Reject");

Liberty BASIC[edit]

NOTE: You have to manually type in ALL of the code since the Input statement will not successfully input data from a paste event even though it will show up in the MainWin.

s$ = "s$ = Input a$ : Print (a$ = Left$(s$, 5) + chr$(34) + s$ + chr$(34) + Mid$(s$, 14, 3) + Mid$(s$, 6, 100)) + Mid$(s$, 23, 3)" : Input a$ : Print (a$ = Left$(s$, 5) + chr$(34) + s$ + chr$(34) + Mid$(s$, 14, 3) + Mid$(s$, 6, 100))


prog = "prog = ``;\nPrint[InputString[] == \n   ToString[StringForm[prog, ToString[prog, InputForm]]]];";
Print[InputString[] ==
ToString[StringForm[prog, ToString[prog, InputForm]]]];


# this is file
local $/;
print do { open 0; <0> } eq <> ? "accept" : "reject";


perl <

Perl 6[edit]

For the narcissist to work you must be very careful with whitespace. The following version works if it is given to standard input as exactly one line terminated by a newline character.

Note how the code takes advantage of Perl 6's ability to nest quoting delimiters.

EVAL my $self = q{say slurp() eq q[EVAL my $self = q{]~$self~q[}]~10.chr ?? q{Beautiful!} !! q{Not my type.}}
$ narcissist='EVAL my $self = q{say slurp() eq q[EVAL my $self = q{]~$self~q[}]~10.chr ?? q{Beautiful!} !! q{Not my type.}}'
$ perl6 -e "$narcissist" <<<"$narcissist"
$ perl6 -e "$narcissist" <<<"$narcissist # a comment ruining it all" 
Not my type.


(de narcissist (Str)
(= Str (str narcissist)) )


: (narcissist "(Str) (= Str (str narcissist))")
-> T


function entropy ($string) {
$n = $string.Length
$string.ToCharArray() | group | foreach{
$p = $_.Count/$n
$i = [Math]::Log($p,2)
} | measure -Sum | foreach Sum
entropy $(get-content "$($MyInvocation.MyCommand.Name )" -Raw)


For Python 2.x:

import sys
with open(sys.argv[0]) as quine:
code = raw_input("Enter source code: ")
if code ==


This shows a REPL interaction, where the second expression is what is typed when the code stops to read some input.

-> ((lambda (x) (equal? (read) (list x (list 'quote x))))
'(lambda (x) (equal? (read) (list x (list 'quote x)))))
((lambda (x) (equal? (read) (list x (list 'quote x))))
'(lambda (x) (equal? (read) (list x (list 'quote x)))))


version 1[edit]

(returns   1   or   0)

/*REXX*/    say arg(1)=sourceline(1)

version 2[edit]

(returns   accept   or   reject)

/*REXX*/    say word('reject accept',1+(arg(1)=sourceline(1)))


Translation of the C version.

s = "s = %s%s%s; puts(gets.chomp == (s %% [34.chr, s, 34.chr]) ? 'accept' : 'reject')"; puts(gets.chomp == (s % [34.chr, s, 34.chr]) ? 'accept' : 'reject')


$ ruby narcissist.rb < narcissist.rb


say ( == ARGF.slurp);


With the use of explicit reflexive introspection:

apply {{} {puts [expr {[gets stdin] eq [info level 0]}]}}

Without such commands, using pure generation of strings and lists:

apply {s {puts [expr {[gets stdin]eq[list {*}$s $s]}]}} {apply {s {puts [expr {[gets stdin]eq[list {*}$s $s]}]}}}


@(bind my64 "QChuZXh0IDphcmdzKQpAZmlsZW5hbWUKQChuZXh0IGAhc2VkIC1uIC1lICcyLCRwJyBAZmlsZW5hbWUgfCBiYXNlNjRgKQpAKGZyZWVmb3JtICIiKQpAaW42NApAKG5leHQgYEBmaWxlbmFtZWApCkBmaXJzdGxpbmUKQChjYXNlcykKQCAgKGJpbmQgZmlyc3RsaW5lIGBAQChiaW5kIG15NjQgIkBteTY0IilgKQpAICAoYmluZCBpbjY0IG15NjQpCkAgIChiaW5kIHJlc3VsdCAiMSIpCkAob3IpCkAgIChiaW5kIHJlc3VsdCAiMCIpCkAoZW5kKQpAKG91dHB1dCkKQHJlc3VsdApAKGVuZCkK")
@(next :args)
@(next `!sed -n -e '2,$p' @filename | base64`)
@(freeform "")
@(next `@filename`)
@ (bind firstline `@@(bind my64 "@my64")`)
@ (bind in64 my64)
@ (bind result "1")
@ (bind result "0")

How to run, showing self-acceptance:

$ txr narcissist.txr narcissist.txr

Informal proof.

We consider what happens if we make an alteration to the code and feed it to the original.

Changing any character of narcissist.txr can be divided into two cases.

  • Case 1: modification is done to line 1. This difference be caught by the @(bind firstline ...) directive later down in the query, causing a matching failure. The first line is verified to have exactly the form that it does, with the given base 64 string embedded.
  • Case 2: modification is done in some other line. This difference will be caught by @(bind in64 my64) because a modification to the data after the first line changes the base 64 string that is computed from that data, making in64 not equivalent to my64, leading to a match failure.

These cases are an exhaustive partitioning of the possibilities; there are no ways to modify the data which do not land into one of these cases.

Nothing in the query calls for any iteration or recursion. Termination depends on the base64 and sed utilities munching through the input, which presumably process an input of size N in O(N) steps. On that note, we could limit how many lines of the input are passed to base64 by using sed -n -e '2,20p'.