I am working on a new code and I am new to F#. How can I implement state machine
Asked
Active
Viewed 161 times
0
-
3Post the code you have tried. – Ryan Wilson May 14 '19 at 18:51
-
1https://gist.github.com/MiloszKrajewski/b0a2668ab10d8b567b89b1b078c02a2f – Alexan May 14 '19 at 20:51
1 Answers
0
It really depends on what kind of state machine do you need - you should write more about your exact problem to specify the issue more precisely.
However, one general and very common pattern of encoding state machines is to use recursive functions to model states and calls to model state transition.
For example, a traffic light with red -> green -> orange -> .. loop can be written as:
let rec red () =
printfn "In red state"
green ()
and green () =
printfn "In green state"
orange ()
and orange () =
printfn "In orange state"
red ()

Tomas Petricek
- 240,744
- 19
- 378
- 553