36 lines
No EOL
932 B
FSharp
36 lines
No EOL
932 B
FSharp
module DFA
|
|
|
|
open System
|
|
|
|
type State = { name: String }
|
|
|
|
type DFA = {
|
|
sigma: Char list
|
|
states: State list
|
|
delta: State -> Char -> State
|
|
beginState: State
|
|
acceptingStates: State list
|
|
}
|
|
|
|
let validateDFA (dfa:DFA) =
|
|
|
|
let allAcceptingStatesAreStates =
|
|
dfa.acceptingStates
|
|
|> List.map (fun x -> List.contains x dfa.states)
|
|
|> List.filter (fun x -> not x)
|
|
|> List.length < 1
|
|
|
|
List.contains dfa.beginState dfa.states ||
|
|
allAcceptingStatesAreStates
|
|
|
|
let processChar (dfa: DFA) (state: State) (char: Char) =
|
|
dfa.delta state char
|
|
|
|
let rec processCharArray (dfa: DFA) (state: State) (charArr: Char list) =
|
|
match charArr with
|
|
| [] -> state
|
|
| _ -> processCharArray dfa (processChar dfa state charArr.Head) charArr.Tail
|
|
|
|
let acceptsWord (dfa: DFA) (word: String) =
|
|
dfa.acceptingStates
|
|
|> List.contains (processCharArray dfa dfa.beginState (Seq.toList word)) |