I have to make a state machine, which equals a text editor's search funtion.
I need to use foldl/foldr to apply the function to every character of a string.
I have a few states, which I have to work with:
type State = Int
start :: State
start = 0
accept :: State
accept = (-2)
reject :: State
reject = (-1)
And I have type synonim : type Definition = [(State, Char -> State)]
The function should look like this: fsm :: Definition -> String -> Bool
My code looks like this right now:
transition :: Char -> State -> (Char -> State)
transition x y z
| x == z = y
| x == '*' = y
| otherwise = reject
transitions :: [(Char, State)] -> (Char -> State)
transitions ((a,b):xs) e
| a == e || a == '*' = b
| a /= e || a /= '*' = transitions xs e
| otherwise = reject
step :: Definition -> State -> Char -> State
step ((a,b):xs) e f
| a == e = b f
| a /= e = step xs e f
| otherwise = reject
It has a starting state, apply transition or transitions function and if it is accepted, the state accepted is the next starting state.
Here is some test cases, which I have to test the function:
fsm [ (start, transition '*' accept)] "x" == True
fsm [ (start, transition 'a' 1)
, (1, transition 'l' 2)
, (2, transition '*' accept)
] "alma" == True
fsm [ (start, transition '*' 1)
, (1, transition '*' 2)
, (2, transition 'x' 3)
, (3, transition '*' accept)
] "taxi" == True
fsm [ (start, transitions [('\n',accept), ('*', 1)])
, (1, transition '*' start)
] "aa\n" == True