Last active
May 18, 2017 07:52
-
-
Save kitofr/b3cb53a6a5234ee9e005fafba0087e00 to your computer and use it in GitHub Desktop.
A simple state machine in Elm - http://daniel-levin.github.io/2015/01/19/primitive-state-machine-in-haskell.html
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
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 | |
] |
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
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