Narcissist: Difference between revisions
(→{{header|Tcl}}: Added a "purer" way to do this) |
(Adding a JS implementation (based on my quine)) |
||
Line 21: | Line 21: | ||
Based upon the quine. |
Based upon the quine. |
||
<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> |
<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> |
||
=={{header|JavaScript}}== |
|||
{{works with|SpiderMonkey|1.7.0}} |
|||
Based upon [[Quine#Using_eval|one of the quines]]. |
|||
<lang javascript>var code='var q=String.fromCharCode(39);print("var code=" + q + code + q + "; eval(code)" == readline() ? 1 : 0)'; eval(code)</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
Revision as of 22:31, 22 September 2010
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, input = stdin and output = stdout. The narcissist should be able to cope with any input, and should not read any data files from disk.
ALGOL 68
<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. <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>
JavaScript
Based upon one of the quines. <lang javascript>var code='var q=String.fromCharCode(39);print("var code=" + q + code + q + "; eval(code)" == readline() ? 1 : 0)'; eval(code)</lang>
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>