Created
January 27, 2017 03:42
-
-
Save jwhiteman/417bd27b8872968e0723e253f71ac74a to your computer and use it in GitHub Desktop.
basic DFA simulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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