Skip to content

Instantly share code, notes, and snippets.

@icalvo
Created January 25, 2017 13:40
Show Gist options
  • Save icalvo/fbad2c8ea99bdbdee9b611267ffa6eb7 to your computer and use it in GitHub Desktop.
Save icalvo/fbad2c8ea99bdbdee9b611267ffa6eb7 to your computer and use it in GitHub Desktop.
F# Mealy FSM model for interactive console applications
// transition:('State -> 'Input -> 'State) -> isFinish:('State -> bool) -> initialState:'State -> input:seq<'Input> -> 'State list
let interactiveConsole transition isFinish initialState input =
input
|> Seq.scan transition initialState
|> Seq.takeWhile (not << isFinish)
|> Seq.toList
// unit -> seq<string>
let consoleInput =
Seq.initInfinite (fun _ -> Console.ReadLine())
|> Seq.takeWhile ((<>) null)
// state:'State -> input:'Input -> 'State
let myTransition state input =
// Write your Mealy-style transition function here
state
// state:'State -> bool
let myIsFinish state =
// Just return true with your finish states
true
[<EntryPoint>]
let main argv =
let myInitialState = 0
// Here we feed the FSM with console input. You can use any list of
// strings, for example for testing or replaying an execution.
// You may have some use for the final list of states, otherwise ignore it.
ignore <| interactiveConsole myTransition myIsFinish myInitialState consoleInput
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment