#Python 3, 352 208 205 bytes
Python 3, 352 208 205 bytes
This is still very ungolfed, and I will try to add an explanation later. After some modifications, I managed to remove 144 147 bytes.
f=lambda x,l=len,r=range:'\n'.join(*x)if l(x)<2 else f([[x[i][j]+['0',''][j<=l(x[i])//2]for j in r(l(x[i]))]+[['?','?<'][l(x)<3]]+[x[i+1][j]+['1',''][j>=l(x[i])//2]for j in r(l(x[i]))]for i in r(0,l(x),2)])
A function f
that takes input of the truth table values as a list of Booleans of the form ['1','1','1','0','0','1'...]
, where '1'
is truthy and '0'
is falsey, and returns a Stackylogic program.
If you want to test a produced program, you can use GamrCorps's Convex interpreter here.
How it works
This is a recursive function and uses the inductive method described in the question.
At the zero-indexed recursion level a
, the function creates n/2
a+1
-input Stackylogic programs from the n
a
-input programs in the list. This is done by joining all adjacent pairs of two programs in the list with ?
; since the cursor is always at the middle element of each constituent program, the required appending of 0
or 1
can be performed by iterating over each line of the programs being joined, and appending if the index of the current line is less than or equal to/greater than or equal to the middle index as appropriate. If the list contains only two programs, the next recursive call will give the final program; since this requires a cursor, joining instead occurs on ?<
.
When the list has length 1
, the list must contain just one element containing the full program. Hence, all lines in the program are joined on a newline, and then returned.
An example helps to illustrate this:
Take the input ['1', '1', '1', '0', '0', '1', '1', '1'].
Level Return value
0 [['1', '?', '1'], ['1', '?', '0'], ['0', '?', '1'], ['1', '?', '1']]
1 [['1', '?', '10', '?', '11', '?', '0'], ['0', '?', '10', '?', '11', '?', '1']]
2 [['1', '?', '10', '?', '110', '?0', '00', '?<', '01', '?1', '101', '?', '11', '?', '1']]
3 '1\n?\n10\n?\n110\n?0\n00\n?<\n01\n?1\n101\n?\n11\n?\n1'
which when printed gives:
1
?
10
?
110
?0
00
?<
01
?1
101
?
11
?
1