Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
basic DFA simulation
defmodule DFA do
def accepts?(accept: accept_states, rules: rules, s: string) do
string
|> String.graphemes
|> List.foldl(1, fn(char, state) -> next(state: state, char: char, rules: rules) end)
|> member?(accept_states)
end
defp next(state: nil, char: _char, rules: _rules) do
nil
end
defp next(state: state, char: char, rules: [{state, char, next_state}|_rest]) do
next_state
end
defp next(state: state, char: char, rules: [_head|rest]) do
next(state: state, char: char, rules: rest)
end
defp next(state: _state, char: _char, rules: []) do
nil
end
defp member?(_state, []), do: false
defp member?(state, [state|_rest]), do: true
defp member?(state, [_head|rest]), do: member?(state, rest)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment