Change from State struct to just strings
This commit is contained in:
parent
fa48bc49d1
commit
30949252c0
4 changed files with 36 additions and 40 deletions
|
|
@ -9,23 +9,23 @@ module ContainsSequence =
|
|||
let contains0101:NFA = {
|
||||
sigma = Seq.toList "01";
|
||||
states = [
|
||||
{name = "q0"};
|
||||
{name = "q1"};
|
||||
{name = "q2"};
|
||||
{name = "q3"}
|
||||
{name = "yes"}
|
||||
"q0";
|
||||
"q1";
|
||||
"q2";
|
||||
"q3"
|
||||
"yes"
|
||||
];
|
||||
delta = (fun x y ->
|
||||
match (x, y) with
|
||||
| ({name = "q0"}, '0') -> [{name = "q0"}; {name = "q1"}]
|
||||
| ({name = "q1"}, '1') -> [{name = "q0"}; {name = "q2"}]
|
||||
| ({name = "q2"}, '0') -> [{name = "q0"}; {name = "q3"}]
|
||||
| ({name = "q3"}, '1') -> [{name = "yes"}]
|
||||
| ({name = "yes"}, _) -> [{name = "yes"}]
|
||||
| ("q0", '0') -> ["q0"; "q1"]
|
||||
| ("q1", '1') -> ["q0"; "q2"]
|
||||
| ("q2", '0') -> ["q0"; "q3"]
|
||||
| ("q3", '1') -> ["yes"]
|
||||
| ("yes", _) -> ["yes"]
|
||||
| _ -> []
|
||||
);
|
||||
beginState = {name = "q0"};
|
||||
acceptingStates = [{name = "yes"}]
|
||||
beginState = "q0";
|
||||
acceptingStates = ["yes"]
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
14
DFA.fs
14
DFA.fs
|
|
@ -2,8 +2,6 @@ module DFA
|
|||
|
||||
open System
|
||||
|
||||
type State = { name: String }
|
||||
|
||||
///
|
||||
/// A deterministic finite automaton
|
||||
///
|
||||
|
|
@ -11,14 +9,14 @@ type DFA = {
|
|||
/// All possible characters
|
||||
sigma: Char list
|
||||
/// All possible states
|
||||
states: State list
|
||||
states: String list
|
||||
/// All possible transitions
|
||||
delta: State -> Char -> State
|
||||
delta: String -> Char -> String
|
||||
/// The state this DFA begins in
|
||||
beginState: State
|
||||
beginState: String
|
||||
/// The states in which this DFA accepts
|
||||
/// the word
|
||||
acceptingStates: State list
|
||||
acceptingStates: String list
|
||||
}
|
||||
|
||||
/// Returns true if the DFA is valid, false otherwise
|
||||
|
|
@ -51,12 +49,12 @@ let validateDFA (dfa:DFA) =
|
|||
|
||||
/// Returns the state the DFA is in after
|
||||
/// reading this character
|
||||
let processChar (dfa: DFA) (state: State) (char: Char) =
|
||||
let processChar (dfa: DFA) (state: String) (char: Char) =
|
||||
dfa.delta state char
|
||||
|
||||
/// Returns the state the DFA is in after
|
||||
/// reading this character array
|
||||
let rec processCharArray (dfa: DFA) (state: State) (charArr: Char list) =
|
||||
let rec processCharArray (dfa: DFA) (state: String) (charArr: Char list) =
|
||||
match charArr with
|
||||
| [] -> state
|
||||
| head :: tail -> processCharArray dfa (processChar dfa state head) tail
|
||||
|
|
|
|||
|
|
@ -9,32 +9,32 @@ module IsNumeric =
|
|||
let numericDFA:DFA = {
|
||||
sigma = Seq.toList "01ab";
|
||||
states = [
|
||||
{name = "yes"};
|
||||
{name = "no"}
|
||||
"yes";
|
||||
"no"
|
||||
];
|
||||
delta = (fun x y ->
|
||||
match (x, y) with
|
||||
| ({name = "yes"}, '0') -> {name = "yes"}
|
||||
| ({name = "yes"}, '1') -> {name = "yes"}
|
||||
| _ -> {name = "no"}
|
||||
| ("yes", '0') -> "yes"
|
||||
| ("yes", '1') -> "yes"
|
||||
| _ -> "no"
|
||||
);
|
||||
beginState = {name = "yes"};
|
||||
acceptingStates = [{name = "yes"}]
|
||||
beginState = "yes";
|
||||
acceptingStates = ["yes"]
|
||||
}
|
||||
|
||||
let numericNFA:NFA = {
|
||||
sigma = Seq.toList "01ab";
|
||||
states = [
|
||||
{name = "yes"}
|
||||
"yes"
|
||||
];
|
||||
delta = (fun x y ->
|
||||
match (x, y) with
|
||||
| ({name = "yes"}, '0') -> [{name = "yes"}]
|
||||
| ({name = "yes"}, '1') -> [{name = "yes"}]
|
||||
| ("yes", '0') -> ["yes"]
|
||||
| ("yes", '1') -> ["yes"]
|
||||
| _ -> []
|
||||
);
|
||||
beginState = {name = "yes"};
|
||||
acceptingStates = [{name = "yes"}]
|
||||
beginState = "yes";
|
||||
acceptingStates = ["yes"]
|
||||
}
|
||||
|
||||
let test =
|
||||
|
|
|
|||
14
NFA.fs
14
NFA.fs
|
|
@ -2,31 +2,29 @@ module NFA
|
|||
|
||||
open System
|
||||
|
||||
type State = { name: String }
|
||||
|
||||
/// A non-deterministic finite automaton
|
||||
type NFA = {
|
||||
/// All possible characters
|
||||
sigma: Char list
|
||||
/// All possible states
|
||||
states: State list
|
||||
states: String list
|
||||
/// All possible transitions
|
||||
delta: State -> Char -> State list
|
||||
delta: String -> Char -> String list
|
||||
/// The state this NFA begins in
|
||||
beginState: State
|
||||
beginState: String
|
||||
/// The states in which this NFA accepts
|
||||
/// the word
|
||||
acceptingStates: State list
|
||||
acceptingStates: String list
|
||||
}
|
||||
|
||||
/// Returns a list of possible states after this charater
|
||||
/// was read
|
||||
let processChar (nfa: NFA) (state: State) (char: Char) =
|
||||
let processChar (nfa: NFA) state (char: Char) =
|
||||
nfa.delta state char
|
||||
|
||||
/// Returns a list of possible states after this character
|
||||
/// list was read
|
||||
let rec processCharArray (nfa: NFA) (states: State list) (charArr: Char list) : State list =
|
||||
let rec processCharArray (nfa: NFA) (states: String list) (charArr: Char list) : String list =
|
||||
match charArr with
|
||||
| [] -> states
|
||||
| head :: tail ->
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue