I am totally newbie on haskell. My java know how just isnt helping me alot.
i need any help or guide on finishing the code. I have tried most of the parts and also indicated comments on what i tend to achieve.
A DFA has been defined as follows (original image of DFA definition):
Q = {q1,q2,q3,q4,q5}
qs = q1
F = {q4}
delta = {
<q1,0,q2>,<q1,1,q2>,<q1,.,q3>,
<q2,0,q2>,<q2,1,q2>,<q2,.,q4>,
<q3,0,q4>,<q3,1,q4>,<q3,.,q5>,
<q4,0,q4>,<q4,1,q4>,<q4,.,q5>,
<q5,0,q5>,<q5,1,q5>,<q5,.,q5>
}
Sigma = {0,1,.}
task:
Create a Haskell language program that can be used to execute any arbitrary Deterministic Finite Automaton corresponding to the FDA definition above. Represent the DFA as a four-tuple
a. represent each state with its name as a String
b. represents all states as a list of states
c. represent each transition as three tuple and the list of transitions as a list.
To assist you, implement the following functions in your solution:
a. stateFactory – returns a DFA definition (i.e. a four tuple)
b. allStates, firstState, finalStates, and allTransitions – take a DFA and return the corresponding component of the DFA. For example, in the above DFA instance, allStates would return the list of states, q₁, q₂, q₃, q₄, and q₅.
c. transFrom, transInput, and transTo – take a transition and return the corresponding component of the transition
d. findTransition – takes a state, a label, and a list of transitions and returns a list containing the single transition matching the given state and character (note the transitions emanating from a state are uniquely determined by each character label
e. findNextState – takes a DFA, a label, and a state and returns a state
f. dfaAccept – takes a DFA and an input String and returns True if the DFA accepts the input and False otherwise (i.e. decompose the string one character at a time, don’t match the entire string since your solution must work for any DFA). It is helpful to separate this into two functions each with different sets of parameters (one for an empty list and one for a non-empty list). One function takes a current state and one simply takes a DFA, whose state is assumed to be the initial state of the DFA.
this is my code has lots of errors but am trying to fix them
allStates = ["q1","q2","q3","q4","q5"] -- iniitialize all states
firstState = "q1"
finalStates = "q4"
--define all transitions as a tuple
t1 = ("q1",'0',"q2")
t2 = ("q1",'1',"q2")
t3 = ("q1",'.',"q3")
-- place all transitions in a list
allTransitions = [t1,t2,t3]
lst =[]
stateFactory = (allStates, firstState, finalStates, allTransitions)
findTransition state label listOfTransition =
if (state , label , "q1") elem listOfTransition
then lst ++ [] -- if the tuple matches the one in transition add to list
else
lst -- no match dont add
if (state , label , "q2") elem listOfTransition
then lst ++ [] -- if the tuple matches the one in transition add to list
else
lst -- no match dont add
if (state , label , "q3") elem listOfTransition
then lst ++ [] -- if the tuple matches the one in transition add to list
else
lst -- no match dont add
findNextState DFA label state =
--get the transition and extract the last element which is the state
last findTransition state label allTransitions
dfaAccept DFA inputString =
if inputString == null then False
expected Output
Prelude> dfaAccept stateFactory “”
False
Prelude> dfaAccept stateFactory “1”
False
Prelude> dfaAccept stateFactory “1.0”
True
Prelude> dfaAccept stateFactory “11.11”
True
Prelude> dfaAccept stateFactory “10.10.10”
False