March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -1,52 +1,18 @@
import std.stdio, std.typecons, std.array, std.algorithm, std.string;
import std.stdio, std.typecons, std.algorithm, std.string, std.array;
void turing(Sy, St)(in Sy[] symbols, in Sy blank, in St initialState,
in St[] haltStates, in St[] runningStates,
in Tuple!(Sy, string, St)[Sy][St] rules,
in Sy[] startingTape = []) {
Sy[int] tape;
foreach (i, sy; startingTape)
tape[i] = sy;
int pos = 0;
St state = initialState;
while (!haltStates.canFind(state)) {
//auto symbol = tape.setDefault(pos, blank);
if (pos !in tape)
tape[pos] = blank;
auto symbol = tape[pos];
tape.keys.sort()
.map!(i => format(["%s", "(%s)"][i == pos], tape[i]))
.join(" ").writeln;
//{const outsym, const action, state} = rules[state][symbol];
const r = rules[state][symbol];
state = r[2];
tape[pos] = r[0];
pos += ["left": -1, "stay": 0, "right": 1][r[1]];
}
void turing(Sy, St)(in St state, Sy[int] tape, in int pos,
in Tuple!(Sy, int, St)[Sy][St] rules) {
if (state.empty) return;
const r = rules[state][tape[pos] = tape.get(pos, Sy.init)];
writefln("%-(%s%)", tape.keys.sort()
.map!(i => format(i == pos ? "(%s)" : " %s ", tape[i])));
tape[pos] = r[0];
turing(r[2], tape, pos + r[1], rules);
}
void main() {
alias t = tuple;
turing(['b', '1'], // Permitted symbols.
'b', // Blank symbol.
"q0", // Starting state.
["qf"], // Terminating states.
["q0"], // Running states.
// Operating rules.
["q0": ['1': t('1', "right", "q0"),
'b': t('1', "stay", "qf")]],
['1', '1', '1']); // Starting tape.
writeln;
turing([0, 1], // Permitted symbols.
0, // Blank symbol.
"a", // Starting state.
["halt"], // Terminating states.
["a", "b", "c"], // Running states.
// Operating rules.
["a": [0: t(1, "right", "b"), 1: t(1, "left", "c")],
"b": [0: t(1, "left", "a"), 1: t(1, "right", "b")],
"c": [0: t(1, "left", "b"), 1: t(1, "stay", "halt")]]);
turing("a", null, 0,
["a": [0: tuple(1, 1, "b"), 1: tuple(1, -1, "c")],
"b": [0: tuple(1, -1, "a"), 1: tuple(1, 1, "b")],
"c": [0: tuple(1, -1, "b"), 1: tuple(1, 0, "")]]);
}