Skip to content

Instantly share code, notes, and snippets.

@okaram
Last active August 29, 2015 14:00
Show Gist options
  • Save okaram/11360491 to your computer and use it in GitHub Desktop.
Save okaram/11360491 to your computer and use it in GitHub Desktop.
Automatas in Scala
class DFA[StateType]
(
val Q:Set[StateType],
val F:Set[StateType],
val q0:StateType,
val delta: (StateType,Char)=>StateType,
val Sigma:Set[Char]
)
{
...
def accepts(input:List[Char]):Boolean =F contains deltaHat(q0,input)
// sample DFA transition function
def dfa1_delta(state:Int,inp:Char)={
state match {
case 0=>
if(inp=='a') 0 else 1;
case 1=>
if(inp=='a') 2 else 1;
case 2=> 2
}
}
def deltaHat(state:StateType,input:List[Char]):StateType= {
input match {
case Nil => state;
case head::tail => deltaHat(delta(state,head),tail)
}
}
val Q=Set(0,1,2);
val F=Set(2);
val d=new DFA(Q,F,0,dfa1_delta,Set('a','b'));
println(d.accepts(List('b','a')));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment