From 30949252c08b6a515cdc219d48fbd97eb9ebe81c Mon Sep 17 00:00:00 2001 From: joachimschmidt557 Date: Wed, 20 Feb 2019 19:31:18 +0100 Subject: [PATCH] Change from State struct to just strings --- ContainsSequence.fs | 24 ++++++++++++------------ DFA.fs | 14 ++++++-------- IsNumericChecker.fs | 24 ++++++++++++------------ NFA.fs | 14 ++++++-------- 4 files changed, 36 insertions(+), 40 deletions(-) diff --git a/ContainsSequence.fs b/ContainsSequence.fs index cb9b34d..d6233e5 100644 --- a/ContainsSequence.fs +++ b/ContainsSequence.fs @@ -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"] } diff --git a/DFA.fs b/DFA.fs index bcd1fa9..8e65203 100644 --- a/DFA.fs +++ b/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 diff --git a/IsNumericChecker.fs b/IsNumericChecker.fs index fb752c2..6418a33 100644 --- a/IsNumericChecker.fs +++ b/IsNumericChecker.fs @@ -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 = diff --git a/NFA.fs b/NFA.fs index c6f307e..c6fc3f3 100644 --- a/NFA.fs +++ b/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 ->