My machine specification is a list of the form
[
[fromState,
[inputSymbol, toState, outputSymbol],
[inputSymbol, toState, outputSymbol],
....
[inputSymbol, toState, outputSymbol]
],
[fromState,
[inputSymbol, toState, outputSymbol],
[inputSymbol, toState, outputSymbol],
....
[inputSymbol, toState, outputSymbol]
],
....
[fromState,
[inputSymbol, toState, outputSymbol],
[inputSymbol, toState, outputSymbol],
....
[inputSymbol, toState, outputSymbol]
]
]
This representation allows me to use assoc to match the current state against one in the table. Once I've found the current state, I can use assoc on the attached list to match the current input symbol, and from there get the next state and the corresponding output symbol.
The program for a imulator using this specification is:
fsmSim(Machine, currentState, [inputSymbol | More]) => [_ | Choices] = assoc(currentState, Machine), // find state [_, toState, outputSymbol] = assoc(inputSymbol, Choices), // find symbol [outputSymbol | fsmSim(Machine, toState, More)]; // produce output and continue fsmSim(Machine, currentState, []) => []; fsmSim(Machine, currentState, _) => ["undefined transition"];
The above code is a rex-executable function.