Narcissist: Difference between revisions

m
→‎{{header|Phix}}: missing /, or type it in
m (→‎{{header|Phix}}: marked p2js incompatible)
m (→‎{{header|Phix}}: missing /, or type it in)
 
(9 intermediate revisions by 6 users not shown)
Line 20:
Took code from [[Quine]], has to be in one line (could be done pretty printed, too, but not as simple).
 
<langsyntaxhighlight Adalang="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;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 27:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''}}
<langsyntaxhighlight lang="algol68">STRINGs="STRINGs="";print(readstring=2*s[:9]+2*s[9:])";print(readstring=2*s[:9]+2*s[9:])</langsyntaxhighlight>
Output: T or F depending on input.
 
Line 34:
As long as the script hasn't been exported as run-only (''ie.'' without its source code):
 
<langsyntaxhighlight lang="applescript">(display dialog "" default answer "")'s text returned = (do shell script ("osadecompile " & (path to me)'s POSIX path's quoted form))</langsyntaxhighlight>
 
{{output}}
Depending on the text entered and button clicked, one of:
<langsyntaxhighlight lang="applescript">true
false
error "User cancelled." number -128</langsyntaxhighlight>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">blk:[print (("blk:" ++ (as .code blk) ++ "do blk") = input "") ? -> "accept" -> "reject"]do blk</syntaxhighlight>
 
{{out}}
 
<pre>$ arturo narcissist.art < narcissist.art
accept</pre>
 
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">Narcissist(Input) {
FileRead, Source, % A_ScriptFullPath
return Input == Source ? "accept" : "reject"
}</langsyntaxhighlight>
'''Example Use:'''
<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox, % Narcissist(FileOpen(A_ScriptFullPath, "r").Read()) "`n"
. Narcissist("This isn't the text you're looking for.")</langsyntaxhighlight>
{{Output}}
<pre>accept
Line 58 ⟶ 66:
{{works with|BBC BASIC for Windows}}
Prints '1' on success and '0' on failure.
<langsyntaxhighlight lang="bbcbasic">INPUT a$:PRINT -(a$=$(PAGE+34)+$(PAGE+33)):REM INPUT a$:PRINT -(a$=$(PAGE+34)+$(PAGE+33)):REM</langsyntaxhighlight>
 
=={{header|Befunge}}==
Line 65 ⟶ 73:
Outputs <tt>1</tt> if the given line of input matches the source code, and <tt>0</tt> if it doesn't.
 
<langsyntaxhighlight lang="befunge">900:0g~>:::0>`#0\#:5#:5#:+#<#~-#g*#0\#:5#+8#1+#\-#!*#-_$$"E"-!>_9-!.@</langsyntaxhighlight>
 
=={{header|C}}==
Based upon the quine. Reads until EOF or newline from stdin, and writes "1" or "0" to stdout.
<langsyntaxhighlight 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)); }</langsyntaxhighlight>
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">
using System;
using System.IO;
Line 101 ⟶ 109:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
Only checks the first line of stdin:
<langsyntaxhighlight lang="lisp">#1=(PRINT (EQUAL (WRITE-TO-STRING '#1# :CIRCLE 1) (READ-LINE *STANDARD-INPUT*)))</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight Dlang="d">import std.file; import std.stdio; import std.string; void main() { auto source = readText("narcissist.d").chomp; auto input = readln().chomp(); if (source == input) writeln("accept"); else writeln("reject"); }</langsyntaxhighlight>
 
=={{header|Déjà Vu}}==
<langsyntaxhighlight lang="dejavu">!. = !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 ) "</langsyntaxhighlight>
 
=={{header|Forth}}==
 
<langsyntaxhighlight lang="forth">: narcissist [ source ] sliteral compare 0= ;</langsyntaxhighlight>
 
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">Dim As String a, s = "Dim As String a, s = Input a : Print (a = Left(s, 21) + Chr(34) + s + Chr(34) + Mid(s, 47, 3) + Mid(s, 23, 108)) + Mid(s, 54, 3)" : Input a : Print (a = Left(s, 21) + Chr(34) + s + Chr(34) + Mid(s, 47, 3) + Mid(s, 23, 108))</langsyntaxhighlight>
 
 
Line 126 ⟶ 134:
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.
 
<langsyntaxhighlight lang="go">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);};}</langsyntaxhighlight>
A version respecting the 80 character line limit:
<langsyntaxhighlight lang="go">package main
 
import (
Line 166 ⟶ 174:
}
 
var x = `</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">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 "</langsyntaxhighlight>
 
=={{header|Huginn}}==
<langsyntaxhighlight lang="huginn">#! /bin/sh
exec huginn --no-argv -E "${0}"
#! huginn
Line 199 ⟶ 207:
print( s == self ? "1\n" : "0\n" );
}
</syntaxhighlight>
</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
{{trans|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 [[Icon%2BUnicon/Intro#Semi-colons| Semi-colons in the language intro]]
<langsyntaxhighlight Iconlang="icon">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</langsyntaxhighlight>
Example:<pre>./narcissist.exe < narcissist.icn
Accept</pre>
Line 210 ⟶ 218:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">#!/j602usr/bin/jconsoleijconsole
main=:3 : 0 NB. tested under j602
self=: '#!/j602/bin/jconsole',LF,'main=:',(5!:5<'main'),LF,'main''''',LF
echo self -: stdin''
)
main''</langsyntaxhighlight>
 
Example use:
 
<syntaxhighlight lang="text">$ ./narcissist.ijs <narcissist.ijs
1
</langsyntaxhighlight>
 
Note that this assumes a suitable os command line.
Line 227 ⟶ 235:
 
'''Alternative solution:'''
<langsyntaxhighlight lang="j"> narcissist=.(-:,~,2#{:)&'(-:,~,2#{:)&'''</langsyntaxhighlight>
 
'''Example use:'''
<langsyntaxhighlight lang="j"> (-:,~,2#{:)&'(-:,~,2#{:)&''' '(-:,~,2#{:)'
0
(-:,~,2#{:)&'(-:,~,2#{:)&''' '(-:,~,2#{:)&''(-:,~,2#{:)&'''''''
1</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Line 262 ⟶ 270:
}
}
</syntaxhighlight>
</lang>
 
=={{header|JavaScript}}==
{{works with|SpiderMonkey|1.7.0}}
Based upon [[Quine#Using_eval|one of the quines]]. Outputs 'true' if source is equal to inputted line (newline terminated), 'false' otherwise.
<langsyntaxhighlight lang="javascript">var code='var q=String.fromCharCode(39);print("var code=" + q + code + q + "; eval(code)" == readline())'; eval(code)</langsyntaxhighlight>
 
{{works with|JScript}}
<langsyntaxhighlight lang="javascript">var oFSO = new ActiveXObject("Scripting.FileSystemObject");
function readfile(fname) {
var h = oFSO.OpenTextFile(fname, 1, false);
Line 290 ⟶ 298:
// compare and contrast
WScript.Echo(self === whatever ? "Accept" : "Reject");
</syntaxhighlight>
</lang>
 
{{works with|Julia|1.2}}
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">mysource = Base.read(Base.source_path(), String)
println(Int(ARGS[1] == mysource))</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.0 (run on Windows 10)
 
fun main(args: Array<String>) {
Line 307 ⟶ 315:
val lines = Array<String>(n) { readLine()!! }
if (lines.joinToString("\r\n") == text) println("\naccept") else println("\nreject")
}</langsyntaxhighlight>
First run (pasting in program text):
{{out}}
Line 341 ⟶ 349:
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.
 
<syntaxhighlight lang="lb">
<lang lb>
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))
</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
{{in}}
<langsyntaxhighlight Mathematicalang="mathematica">prog = "prog = ``;\nPrint[InputString[] == \n ToString[StringForm[prog, ToString[prog, InputForm]]]];";
Print[InputString[] == ToString[StringForm[prog, ToString[prog, InputForm]]]];</langsyntaxhighlight>
{{out}}
<pre>True</pre>
 
=={{header|Nanoquery}}==
<langsyntaxhighlight Nanoquerylang="nanoquery">import Nanoquery.IO
 
// get a file tied to this program's source
Line 377 ⟶ 385:
 
// if we got here, the lines are the same
println "Accept"</langsyntaxhighlight>
 
=={{header|Nim}}==
Program written on a single line to avoid problems with indentation. But we wanted to avoid the too easy solution consisting to read the source file. Input should be terminated by an end of file (Ctrl-D on Linux).
<syntaxhighlight lang="nim">
<lang Nim>
import strutils; let a = "import strutils; let a = $#; echo ord(stdin.readAll == a % (chr(34) & a & chr(34)) & chr(10))"; echo ord(stdin.readAll == a % (chr(34) & a & chr(34)) & chr(10))
</syntaxhighlight>
</lang>
 
{{out}}
Line 394 ⟶ 402:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">narcissist()=input()==narcissist</langsyntaxhighlight>
Run narcissist():<pre>Hello
0</pre>
Line 401 ⟶ 409:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl"># this is file narc.pl
local $/;
print do { open 0; <0> } eq <> ? "accept" : "reject";</langsyntaxhighlight>
Run:
<syntaxhighlight lang="text">perl narc.pl < narc.pl</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<lang Phix>(notonline)-->
<syntaxhighlight lang="phix">
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span>
printf(1,"\n\n%t\n\n",get_text(command_line()[2],GT_LF_LAST)[1]=gets(0))
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"\n\ntrue\n\n"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n\nfalse\n\n"</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">+(</span><span style="color: #7060A8;">gets</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">command_line</span><span style="color: #0000FF;">()[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"r"</span><span style="color: #0000FF;">))!=</span><span style="color: #7060A8;">gets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">))])</span>
</syntaxhighlight>
<!--</lang>-->
{{out}}
Can be run using <code>p test.exw < test.exw</code> or the input manually typed in
<pre>
true
</langpre>
<small>Aside: I suppose there should really be a leading <code>without js -- file i/o</code>, but I wanted to keep it a 1-liner.<br>
Interpreted-only, as is: to allow a compiled version to run, change <code>command_line()[2]</code> to (say) <code>"test.exw"</code><br>
or maybe <code>substitute(command_line()[2],".exe",".exw")</code>, and ship with/install the source code file.</small>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de narcissist (Str)
(= Str (str narcissist)) )</langsyntaxhighlight>
Output:
<pre>: (narcissist "(Str) (= Str (str narcissist))")
Line 422 ⟶ 438:
=={{header|PowerShell}}==
{{works with|PowerShell|2}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Narcissist
{
Line 429 ⟶ 445:
Else { 'Reject' }
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
Narcissist 'Banana'
Line 440 ⟶ 456:
 
'@
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 449 ⟶ 465:
=={{header|Python}}==
{{Works with|Python|2}}
<syntaxhighlight lang="python">
<lang Python>
import sys
with open(sys.argv[0]) as quine:
Line 457 ⟶ 473:
else:
print("Reject")
</syntaxhighlight>
</lang>
 
{{Works with|Python|3}}
<syntaxhighlight lang="python">
<lang Python>
_='_=%r;print (_%%_==input())';print (_%_==input())
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
Line 468 ⟶ 484:
As a dialogue in the Quackery shell. (REPL)
 
<langsyntaxhighlight Quackerylang="quackery">Welcome to Quackery.
 
Enter "leave" to leave the shell.
Line 487 ⟶ 503:
...
 
Cheerio.</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 494 ⟶ 510:
typed when the code stops to read some input.
 
<syntaxhighlight lang="racket">
<lang Racket>
-> ((lambda (x) (equal? (read) (list x (list 'quote x))))
'(lambda (x) (equal? (read) (list x (list 'quote x)))))
Line 500 ⟶ 516:
'(lambda (x) (equal? (read) (list x (list 'quote x)))))
#t
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 509 ⟶ 525:
Note how the code takes advantage of Raku's ability to nest quoting delimiters.
 
<syntaxhighlight lang="raku" perl6line>EVAL my $self = q{say slurp() eq q[EVAL my $self = q{]~$self~q[}]~10.chr ?? q{Beautiful!} !! q{Not my type.}}</langsyntaxhighlight>
 
{{out}}
Line 522 ⟶ 538:
===version 1===
(returns &nbsp; '''1''' &nbsp; or &nbsp; '''0''')
<langsyntaxhighlight lang="rexx">/*REXX*/ say arg(1)=sourceline(1)</langsyntaxhighlight>
 
===version 2===
(returns &nbsp; '''accept''' &nbsp; or &nbsp; '''reject''')
<langsyntaxhighlight lang="rexx">/*REXX*/ say word('reject accept',1+(arg(1)=sourceline(1)))</langsyntaxhighlight>
 
≪ ‘NAR6’ RCL == ≫
‘NAR6’ STO
{{in}}
<pre>
≪ ‘NAR6’ RCL == ≫ NAR6
</langpre>
{{out}}
<pre>
1: 1
</langpre>
=={{header|RPL}}==
« LASTARG RCL →STR == » '<span style="color:blue">NARCISSIST</span>' STO
 
"« LASTARG RCL →STR == »" '<span style="color:blue">NARCISSIST</span>' EVAL <span style="color:grey">@ returns 1</span>
"string" '<span style="color:blue">NARCISSIST</span>' EVAL <span style="color:grey">@ returns 0</span>
=={{header|Ruby}}==
Translation of the C version.
<langsyntaxhighlight 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')</langsyntaxhighlight>
Output:
<pre>$ ruby narcissist.rb < narcissist.rb
Line 536 ⟶ 567:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::io::{stdin, prelude::*};
fn main() {
let src = include_str!("main.rs");
Line 545 ⟶ 576:
.expect("Could not read from STDIN");
println!("{}", src == input);
}</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">import scala.io.StdIn
 
object Narcissist extends App {
Line 558 ⟶ 589:
}
if (lines.mkString("\r\n") == text) println("\naccept") else println("\nreject")
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">say (File.new(__FILE__).open_r.slurp == ARGF.slurp);</langsyntaxhighlight>
 
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">#! /usr/bin/swift
import Foundation
 
Line 577 ⟶ 608:
} else {
print("Reject")
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
With the use of explicit reflexive introspection:
<langsyntaxhighlight lang="tcl">apply {{} {puts [expr {[gets stdin] eq [info level 0]}]}}</langsyntaxhighlight>
Without such commands, using pure generation of strings and lists:
<langsyntaxhighlight lang="tcl">apply {s {puts [expr {[gets stdin]eq[list {*}$s $s]}]}} {apply {s {puts [expr {[gets stdin]eq[list {*}$s $s]}]}}}</langsyntaxhighlight>
 
=={{header|TXR}}==
 
<langsyntaxhighlight lang="txr">@(bind my64 "QChuZXh0IDphcmdzKUBmaWxlbmFtZUAobmV4dCBmaWxlbmFtZSlAZmlyc3RsaW5lQChmcmVlZm9ybSAiIilAcmVzdEAoYmluZCBpbjY0IEAoYmFzZTY0LWVuY29kZSByZXN0KSlAKGNhc2VzKUAgIChiaW5kIGZpcnN0bGluZSBgXEAoYmluZCBteTY0ICJAbXk2NCIpYClAICAoYmluZCBpbjY0IG15NjQpQCAgKGJpbmQgcmVzdWx0ICIxIilAKG9yKUAgIChiaW5kIHJlc3VsdCAiMCIpQChlbmQpQChvdXRwdXQpQHJlc3VsdEAoZW5kKQ==")
@(next :args)
@filename
Line 605 ⟶ 636:
@result
@(end)
</syntaxhighlight>
</lang>
 
{{out}}
Line 613 ⟶ 644:
 
=={{header|UNIX Shell}}==
<langsyntaxhighlight lang="bash">cmp "$0" >/dev/null && echo accept || echo reject</langsyntaxhighlight>
 
=={{header|VBA}}==
Based on the quine
<langsyntaxhighlight lang="vb">Public Sub narcissist()
quote = Chr(34)
comma = Chr(44)
Line 658 ⟶ 689:
Next i
Debug.Print IIf(flag, 1, 0)
End Sub</langsyntaxhighlight>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">import "os" for Process, Platform
import "io" for File, Stdin, Stdout
 
Line 680 ⟶ 711:
} else {
System.print("reject")
}</langsyntaxhighlight>
 
{{out}}
Line 695 ⟶ 726:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">stdin:=File.stdin.read();
thisFileSrc:=File(System.argv[1]).read();
println((stdin==thisFileSrc) and "input matches "+System.argv[1] or "No match");</langsyntaxhighlight>
Since zkl is [usually] compile-when-run, we just compare the contents of source file to stdin.
{{out}}
7,794

edits