A convenient Turing machine
This repository contains following packages:
A tape contains a
, b
and c
symbols. The issue is to replace all b
symbols by *
symbol.
This example demonstrates an issue solving.
import {
Alphabet,
State,
Tape,
TapeBlock,
TuringMachine,
haltState,
ifOtherSymbol,
movements,
} from '@turing-machine-js/machine';
const alphabet = new Alphabet([' ', 'a', 'b', 'c', '*']);
const tape = new Tape({
alphabet,
symbols: ['a', 'b', 'c', 'b', 'a'],
});
const tapeBlock = TapeBlock.fromTapes([tape]);
const machine = new TuringMachine({
tapeBlock,
});
console.log(tape.symbols.join('').trim()); // abcba
machine.run({
initialState: new State({
[tapeBlock.symbol(['b'])]: {
command: [
{
symbol: '*',
movement: movements.right,
},
],
},
[tapeBlock.symbol([tape.alphabet.blankSymbol])]: {
command: [
{
movement: movements.left,
},
],
nextState: haltState,
},
[ifOtherSymbol]: {
command: [
{
movement: movements.right,
},
],
},
}),
});
console.log(tape.symbols.join('').trim()); // a*c*a
S
stands for the initial stateH
stands for thehaltState
- Current state: S
- Current symbol: 'a'
- Transition: 'a'/S,R
[ abcba ] [ abcba ]
^ >> ^
- Current state: S
- Current symbol: 'b'
- Transition: '*'/S,R
[ abcba ] [ a*cba ]
^ >> ^
- Current state: S
- Current symbol: 'c'
- Transition: 'c'/S,R
[ a*cba ] [ a*cba ]
^ >> ^
- Current state: S
- Current symbol: 'b'
- Transition: '*'/S,R
[ a*cba ] [ a*c*a ]
^ >> ^
- Current state: S
- Current symbol: 'a'
- Transition: 'a'/S,R
[ a*c*a ] [ a*c*a ]
^ >> ^
- Current state: S
- Current symbol: ' '
- Transition: ' '/H,L
[ a*c*a ] [ a*c*a ]
^ >> ^