0

I am working on a new code and I am new to F#. How can I implement state machine

It's Addie
  • 17
  • 1

1 Answers1

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