Narcissist: Difference between revisions

1,431 bytes removed ,  8 years ago
→‎{{header|TXR}}: Modernize: no reliance on external tools; @ is backslash-escaped in quasiliteral. Remove "informal proof".
(→‎{{header|Python}}: Fixed Python 3)
(→‎{{header|TXR}}: Modernize: no reliance on external tools; @ is backslash-escaped in quasiliteral. Remove "informal proof".)
Line 329:
 
=={{header|TXR}}==
{{Uses from|GNU coreutils|base64 utility}}
{{Uses from|GNU coreutils|sed utility}}
 
<lang txr>@(bind my64 "QChuZXh0IDphcmdzKUBmaWxlbmFtZUAobmV4dCBmaWxlbmFtZSlAZmlyc3RsaW5lQChmcmVlZm9ybSAiIilAcmVzdEAoYmluZCBpbjY0IEAoYmFzZTY0LWVuY29kZSByZXN0KSlAKGNhc2VzKUAgIChiaW5kIGZpcnN0bGluZSBgXEAoYmluZCBteTY0ICJAbXk2NCIpYClAICAoYmluZCBpbjY0IG15NjQpQCAgKGJpbmQgcmVzdWx0ICIxIilAKG9yKUAgIChiaW5kIHJlc3VsdCAiMCIpQChlbmQpQChvdXRwdXQpQHJlc3VsdEAoZW5kKQ==")
<lang txr>@(bind my64 "QChuZXh0IDphcmdzKQpAZmlsZW5hbWUKQChuZXh0IGAhc2VkIC1uIC1lICcyLCRwJyBAZmlsZW5hbWUgfCBiYXNlNjRgKQpAKGZyZWVmb3JtICIiKQpAaW42NApAKG5leHQgYEBmaWxlbmFtZWApCkBmaXJzdGxpbmUKQChjYXNlcykKQCAgKGJpbmQgZmlyc3RsaW5lIGBAQChiaW5kIG15NjQgIkBteTY0IilgKQpAICAoYmluZCBpbjY0IG15NjQpCkAgIChiaW5kIHJlc3VsdCAiMSIpCkAob3IpCkAgIChiaW5kIHJlc3VsdCAiMCIpCkAoZW5kKQpAKG91dHB1dCkKQHJlc3VsdApAKGVuZCkK")
@(next :args)
@filename
@(next `!sed -n -e '2,$p' @filename | base64`)
@firstline
@(freeform "")
@rest
@in64
@(bind in64 @(base64-encode rest))
@(next `@filename`)
@firstline
@(cases)
@ (bind firstline `@\@(bind my64 "@my64")`)
@ (bind in64 my64)
@ (bind result "1")
Line 349 ⟶ 347:
@(output)
@result
@(end)</lang>
</lang>
 
{{out}}
 
How to run, showing self-acceptance:
<pre>$ txr narcissist.txr narcissist.txr
1</pre>
 
Informal proof.
 
We consider what happens if we make an alteration to the code and feed it to the original.
 
Changing any character of <code>narcissist.txr</code> can be divided into two cases.
 
* Case 1: modification is done to line 1. This difference be caught by the <code>@(bind firstline ...)</code> 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 <code>@(bind in64 my64)</code> because a modification to the data after the first line changes the base 64 string that is computed from that data, making <code>in64</code> not equivalent to <code>my64</code>, 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 <code>sed -n -e '2,20p'</code>.
 
 
{{omit from|bc|Cannot read a file.}}
543

edits