Name: Anonymous 2011-02-20 23:44
Since TeX is Turing-complete, even in the limited form allowed on /sci/, I decided to implement arithmetic (binary number representation) on 4chan.
A test on /sci/:
http://boards.4chan.org/sci/res/2576104
A test on /sci/:
http://boards.4chan.org/sci/res/2576104
\def \I #1{#1}
\def \A #1#2{#2 {#1}}
\def \Set #1#2{\def #2 {\A{#1}}}
\def \Infix #1#2#3{#3 {#1 {#2}}}
\def \DefI #1#2{\def #1 {\Infix #2} \def #2}
\def \L #1#2{#1}
\def \R #1#2{#2}
\def \T #1{#1 \L}
\def \F #1{#1 \R}
\def \PBool #1{#1 {True} {False}}
\def \Not #1{#1 \F \T}
\def \And #1#2{#1 {#2} \F}
\def \Or #1#2{#1 \T {#2}}
\def \Xor #1#2{#1 {#2 \Not} {#2}}
\def \EqB #1#2{#1 {#2} {#2 \Not}}
\def \Maj #1#2{#1 {#2 \Or} {#2 \And}}
\def \CompB #1#2#3#4#5{
#1 {
#2 \I {#4} {#3}
}{
#2 \I {#5} {#4}
}
}
\def \P #1#2#3#4{#3 {#4 {#1}} {#4 {#2}}}
\def \N #1#2{\T}
\DefI \. \PairP #1#2#3{#3 {\P{#1}{#2}}}
\Set \N \Nil
\def \List {\Nil \.}
\def \Left #1{#1 \L}
\def \Right #1{#1 \R}
\def \Null #1{#1 \L {\L \F}}
\def \Lone #1{#1 \L \Null}
\def \Last #1{\Lone {#1} \I {#1 \R} {#1 \L \Last}}
\List \T \Set \NegOne
\List \F \Set \0
\List \F \. \T \Set \1
\def \ShiftR #1{\Lone {#1} \I {\A {#1}} {#1 \L}}
\DefI \NB \NBP #1#2{
\Lone {#1} \And {\EqB {#2} {#1 \R}} \I
{\A {#1}}
{\PairP {#1} {#2}}
}
\def \PBin #1{
\Null {#1} \I
{}
{#1 \L \PBin #1 \R \I 1 0}
}
\DefI \= \EqIP #1#2{
#1 \R \EqB {#2 \R} \And {
\Lone {#1} \And {\Lone {#2}} \Or {
\Lone {#1} \Not \And {\Lone {#2} \Not} \And
{\ShiftR {#1} \= {\ShiftR {#2}}}
}
}
}
\DefI \CompI \CompIP #1#2#3#4#5{
\Lone {#1} \And {\Lone {#2}} \I {
#1 \R \CompB {#2 \R} {#5} {#4} {#3}
}{
\ShiftR {#2} {\ShiftR {#1} \CompIP}
{#3}
{#1 \R \CompB {#2 \R} {#3} {#4} {#5}}
{#5}
}
}
\def \< #1#2{\CompI {#1} {#2} \F \F \T}
\def \AddC #1#2#3{
\Lone {#1} \And {\EqB{#3} {#1 \R}} \I {
\A {#2}
}{
\Lone {#2} \And {\EqB{#3} {#2 \R}} \I {
\A {#1}
}{
\Maj {#3} {#1 \R} {#2 \R}
{\ShiftR {#2} {\ShiftR {#1} \AddC}}
\NB
{\Xor {#3} {#1 \R} \Xor {#2 \R}}
}
}
}
\def \+ #1#2{#2 \AddC {#1} \R}
\1 \+ \1 \Set \2
\2 \+ \1 \Set \3
\3 \+ \1 \Set \4
\4 \+ \1 \Set \5
\5 \+ \1 \Set \6
\6 \+ \1 \Set \7
\7 \+ \1 \Set \8
\8 \+ \1 \Set \9
\9 \+ \1 \Set \Ten
\def \OComp #1{
\Null {#1} \I {
\Nil
}{
#1 \L \OComp \. {#1 \R \Not}
}
}
\def \Neg #1{\OComp{#1} \+ \1}
\def \- #1#2{#2 \OComp \AddC {#1} \L}
\DefI \* \TimesP #1#2{
\Lone {#1} \I {
#1 \R \I {\Neg {#2}}{\0}
}{
#1 \R \I {
#1 \L \TimesP {#2} \NB \F \+ {\A {#2}}
}{
#1 \L \TimesP {#2} \NB \F
}
}
}
\def \QRPos #1#2{
\Lone {#1} \I {
#1 \R \I {
\NegOne \. {\- {#2} \1}
}{
\0 \. \0
}
}{
\ShiftR {#1} \QRPos {#2} \Set \PrevQR
\PrevQR \Right \NB {#1 \R} \Set \NewR
\NewR \< {\A {#2}} \I {
\PrevQR \Left \NB \F \. \NewR
}{
\PrevQR \Left \NB \T \. {\NewR \- {\A {#2}}}
}
}
}
\DefI \QR \QRP #1#2{
\Last {#2} \I {
\Neg {#2} {\Neg {#1} \QRPos} \Set \PrevQR
\PrevQR \Left \. {\PrevQR \Right \Neg}
}{
\= {#2} \0 \I {
\OhShi
}{
\QRPos {#1} {#2}
}
}
}
\def \/ #1#2{\QR {#1} {#2} \Left}
\def \Mod #1#2{\QR {#1} {#2} \Right}
\def \Fact #1{
\< {#1} \2 \I {
\1
}{
\- {#1} \1 \Fact \* {\A {#1}}
}
}
\DefI \At \AtP #1#2{
\= {#2} \0 \I {
#1 \R
}{
\- {#2} \1 {#1 \L \AtP}
}
}
\List {\A9}\.{\A8}\.{\A7}\.{\A6}\.{\A5}\.{\A4}\.{\A3}\.{\A2}\.{\A1}\.{\A0} \Set \Digits
\def \PDecQR #1{
#1 \L \Lone \I {
}{
#1 \L \PDecPos
}
\Digits \At {#1 \R} \I
}
\def \PDecPos #1{
\Ten {\QRPos {#1}} \PDecQR
}
\def \PDec #1{
\< {#1} \0 \I {
- \Neg {#1} \PDecPos
}{
\PDecPos {#1}
}
}