INPUT "Enter first integer: ", a
INPUT "Enter second integer: ", b
c = -1
INPUT "Enter operation. 1 for add, 2 for multiply, 3 for exponent: ", c
d = "+"
e = "s[k[s]][s[k[s[k[s]]]][s[k[k]]]]"
d = "*"
e = "s[k[s]][k]"
d = "^"
e = "s[k[s[s[k][k]]]][k]"
PRINT "Translation of problem: " PRINT NumberPrefix$
(a
); e; NumberPrefix$
(b
) PRINT "Press any key to compute..."
PRINT InterpretInteger&
(EvalLoop$
(SumPrefix$
(a
, b
) + "[s][k]")) PRINT "Result:"; a; d; b;
"="; a
+ b
PRINT InterpretInteger&
(EvalLoop$
(ProductPrefix$
(a
, b
) + "[s][k]")) PRINT "Result:"; a; d; b;
"="; a
* b
PRINT InterpretInteger&
(EvalLoop$
(ExponentPrefix$
(a
, b
) + "[s][k]")) PRINT "Result:"; a; d; b;
"="; a
^ b
'' Identity
'PRINT EvalLoop$("s[k][k]" + "[a]")
'' Wolfram's example
'PRINT EvalLoop$("s[s[k[s]][s[k[k]][s[k[s]][k]]]][s[k[s[s[k][k]]]][k]]")
'PRINT EvalLoop$("s[s[k[s]][s[k[k]][s[k[s]][k]]]][s[k[s[s[k][k]]]][k]][a][b][c]")
'' Zero
'PRINT EvalLoop$("s[k]" + "[s][k]")
'PRINT EvalLoop$(NumberPrefix$(0) + "[s][k]")
'' One
'PRINT EvalLoop$("s[s[k[s]][k]][s[k]]" + "[s][k]")
'PRINT EvalLoop$("s[s[k[s]][k]]" + "[s[k]][s][k]")
'PRINT EvalLoop$(NumberPrefix$(1) + "[s][k]")
'' Two
'PRINT EvalLoop$("s[s[k[s]][k]][s[s[k[s]][k]][s[k]]]" + "[s][k]")
'PRINT EvalLoop$("s[s[k[s]][k]]" + "[" + "s[s[k[s]][k]]" + "[s[k]]][s][k]")
'PRINT EvalLoop$(NumberPrefix$(2) + "[s][k]")
'' Three
'PRINT EvalLoop$("s[s[k[s]][k]][s[s[k[s]][k]][s[s[k[s]][k]][s[k]]]]" + "[s][k]")
'PRINT EvalLoop$("s[s[k[s]][k]]" + "[" + "s[s[k[s]][k]]" + "[" + "s[s[k[s]][k]]" + "[s[k]]]][s][k]")
'PRINT EvalLoop$(NumberPrefix$(3) + "[s][k]")
'' Sum
'PRINT EvalLoop$("s[k[s]][s[k[s[k[s]]]][s[k[k]]]][s[s[k[s]][k]][s[k]]][s[s[k[s]][k]][s[s[k[s]][k]][s[k]]]][s][k]")
'PRINT EvalLoop$(SumPrefix$(1, 2) + "[s][k]")
'PRINT EvalLoop$(SumPrefix$(3, 4) + "[s][k]")
'PRINT EvalLoop$(SumPrefix$(30, 40) + "[s][k]")
'' Product
'PRINT EvalLoop$("s[k[s]][k][s[s[k[s]][k]][s[s[k[s]][k]][s[k]]]][s[s[k[s]][k]][s[s[k[s]][k]][s[k]]]][s][k]")
'PRINT EvalLoop$(ProductPrefix$(2, 2) + "[s][k]")
'PRINT EvalLoop$(ProductPrefix$(3, 2) + "[s][k]")
'PRINT EvalLoop$(ProductPrefix$(2, 3) + "[s][k]")
'' Exponent
'PRINT EvalLoop$(ExponentPrefix$(3, 2) + "[s][k]")
'' Self-apply: siix
'PRINT EvalLoop$("s[s[k][k]][s[k][k]][x]")
'' Infinite loop: sii(sii)
'PRINT EvalLoop$("s[s[k][k]][s[k][k]][s[s[k][k]][s[k][k]]]")
'' Swap: s(k(si))(s(kk)i)ab
'PRINT EvalLoop$("s[k[s[s[k][k]]]][s[k[k]][s[k][k]]][a][b]")
'' Self-reference 1:
'PRINT EvalLoop$("s[k[x]][s[s[k][k]][s[k][k]]][y]")
'' Self-reference 2: (infinite loop)
'PRINT EvalLoop$("s[k[x]][s[s[k][k]][s[k][k]]][s[k[x]][s[s[k][k]][s[k][k]]]]")
' Human helper fucntion(s)
TheReturn = 0
w = t
TheReturn = TheReturn + 1
InterpretInteger& = TheReturn
' Math functions
ExponentPrefix$ = "s[k[s[s[k][k]]]][k]" + "[" + NumberPrefix$(a) + "]" + "[" + NumberPrefix$(b) + "]"
ProductPrefix$ = "s[k[s]][k]" + "[" + NumberPrefix$(a) + "]" + "[" + NumberPrefix$(b) + "]"
SumPrefix$ = "s[k[s]][s[k[s[k[s]]]][s[k[k]]]]" + "[" + NumberPrefix$(a) + "]" + "[" + NumberPrefix$(b) + "]"
NumberPrefix$ = Nest$("s[s[k[s]][k]]", "s[k]", n)
' Higher-order functions
TheReturn = x
TheReturn = f + "[" + TheReturn + "]"
Nest$ = TheReturn
' Eval functions
Tmp = TheStringIn
TheReturn = TheStringIn
k = 0
Tmp = EvalStep$(TheReturn)
k = k + 1
TheReturn = Tmp
'_DELAY .025
EvalLoop$ = TheReturn
TheReturn = TheStringIn
s0 = FindValidS(TheStringIn, ArgListS())
k0 = FindValidK(TheStringIn, ArgListK())
t1 = "s" + ArgListS(1) + ArgListS(2) + ArgListS(3)
t2 = Shave$(ArgListS(1)) + "[" + Shave$(ArgListS(3)) + "][" + Shave$(ArgListS(2)) + "[" + Shave$(ArgListS(3)) + "]]"
TheReturn = Replace$(TheStringIn, t1, t2)
t1 = "k" + ArgListK(1) + ArgListK(2)
t2 = Shave$(ArgListK(1))
TheReturn = Replace$(TheStringIn, t1, t2)
EvalStep$ = TheReturn
' Parsing functions
TheReturn = TheStringIn
TheReturn
= LEFT$(TheReturn
, LEN(TheReturn
) - 1) TheReturn
= RIGHT$(TheReturn
, LEN(TheReturn
) - 1) Shave$ = TheReturn
k
= INSTR(TheStringIn
, TargetSegment
) TheReturn
= LEFT$(TheStringIn
, k
- 1) + NewSegment
+ RIGHT$(TheStringIn
, LEN(TheStringIn
) - k
- LEN(TargetSegment
) + 1) TheReturn = TheStringIn
Replace$ = TheReturn
TheString = TheStringIn
j0 = 0
j1 = 0
j2 = 0
bal = 0
i = StartPos
i = i + 1
bal = bal + 1
bal = bal - 1
j0 = j0 + 1
arr
(j0
) = MID$(TheString
, j1
, j2
- j1
+ 1) j1 = 0
j2 = 0
bal = 0
TheReturn = 2147483647
Tmp = TheStringIn
arr(j) = ""
n = 0
CALL FindArgs
(Tmp
, j
, 3, arr
()) IF ((arr
(1) <> "") AND (arr
(2) <> "") AND (arr
(3) <> "")) THEN TheReturn = j + n
n
= LEN(TheStringIn
) - LEN(Tmp
) FindValidS& = TheReturn
TheReturn = 2147483647
Tmp = TheStringIn
arr(j) = ""
n = 0
CALL FindArgs
(Tmp
, j
, 2, arr
()) TheReturn = j + n
n
= LEN(TheStringIn
) - LEN(Tmp
) FindValidK& = TheReturn