Skip to content

Instantly share code, notes, and snippets.

@kitofr
Last active May 18, 2017 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitofr/b3cb53a6a5234ee9e005fafba0087e00 to your computer and use it in GitHub Desktop.
Save kitofr/b3cb53a6a5234ee9e005fafba0087e00 to your computer and use it in GitHub Desktop.
test =
describe "State machine"
[
test "[A,A,A] should be False" <|
\() ->
Expect.equal (decide [A, A, A]) False
, test "[A,A,A,A,B,B,A,A,A] should be True" <|
\() ->
Expect.equal (decide [A,A,A,A,B,B,A,A,A]) True
, test "[B,A,B,A,B,A] should be False" <|
\() ->
Expect.equal (decide [B,A,B,A,B,A]) False
]
type State = S0 | S1 | S2
type Event = A | B
accepts : State -> (List Event) -> Bool
accepts state action =
case state of
S0 -> case action of
A :: xs -> accepts S1 xs
B :: xs -> accepts S2 xs
_ -> False
S1 -> case action of
A :: xs -> accepts S2 xs
B :: xs -> accepts S0 xs
_ -> False
S2 -> case action of
A :: xs -> accepts S0 xs
B :: xs -> accepts S2 xs
_ -> True
decide : (List Event) -> Bool
decide = accepts S0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment