Narcissist: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|REXX}}: added comments for the versions.)
(Added Perl example)
Line 48: Line 48:
Based upon [[Quine#Using_eval|one of the quines]]. Outputs 'true' if source is equal to inputted line (newline terminated), 'false' otherwise.
Based upon [[Quine#Using_eval|one of the quines]]. Outputs 'true' if source is equal to inputted line (newline terminated), 'false' otherwise.
<lang javascript>var code='var q=String.fromCharCode(39);print("var code=" + q + code + q + "; eval(code)" == readline())'; eval(code)</lang>
<lang javascript>var code='var q=String.fromCharCode(39);print("var code=" + q + code + q + "; eval(code)" == readline())'; eval(code)</lang>

=={{header|Perl}}==
<lang perl># this is file narc.pl
print do { local $/; open 0, $0 or die $!; <0> } eq <> ? "accept" : "reject"</lang>
Run:
<lang>perl narc.pl < narc.pl</lang>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==

Revision as of 13:53, 20 January 2011

Task
Narcissist
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.

Ada

Works with: Ada 2005

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

<lang Ada>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;</lang>

ALGOL 68

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

<lang algol68>STRINGs="STRINGs="";print(readstring=2*s[:9]+2*s[9:])";print(readstring=2*s[:9]+2*s[9:])</lang> Output: T or F depending on input.

C

Based upon the quine. Reads until EOF or newline from stdin, and writes "1" or "0" to stdout. <lang c>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)); }</lang>

J

<lang j>#!/j602/bin/jconsole main=:3 : 0

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

) main</lang>

Example use:

<lang>$ ./narcissist.ijs <narcissist.ijs 1

  </lang>

Note that this assumes a suitable os command line.

JavaScript

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. <lang javascript>var code='var q=String.fromCharCode(39);print("var code=" + q + code + q + "; eval(code)" == readline())'; eval(code)</lang>

Perl

<lang perl># this is file narc.pl print do { local $/; open 0, $0 or die $!; <0> } eq <> ? "accept" : "reject"</lang> Run: <lang>perl narc.pl < narc.pl</lang>

PicoLisp

<lang PicoLisp>(de narcissist (Str)

  (= Str (str narcissist)) )</lang>

Output:

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

REXX

version 1

(returns 1 or 0) <lang rexx> /*REXX*/ say arg(1)=sourceline(1) </lang>

version 2

(returns accept or reject) <lang rexx> /*REXX*/ say word('reject accept',1+(arg(1)=sourceline(1))) </lang>

Ruby

Translation of the C version. <lang ruby>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')</lang> Output:

$ ruby narcissist.rb < narcissist.rb
accept

Tcl

With the use of explicit reflexive introspection: <lang tcl>apply {{} {puts [expr {[gets stdin] eq [info level 0]}]}}</lang> Without such commands, using pure generation of strings and lists: <lang tcl>apply {s {puts [expr {[gets stdin]eq[list {*}$s $s]}]}} {apply {s {puts [expr {[gets stdin]eq[list {*}$s $s]}]}}}</lang>