One of the main difficulties of implementing user interfaces in games is the gap between event-based user interfaces—nowadays the prevalent type—and frame-based simulation frameworks used for games. User interfaces are typically designed to react to user input and remain asleep between user actions. Frame-based simulation divides time into small, discrete quantities and repeatedly updates the state of the simulation.
User interaction often requires a number of steps to be spread out over longer periods, up to several seconds or minutes. Long interactions have to be broken into smaller units of work, which are chained and executed in each frame. Keeping track of the current unit of work and its data commonly leads to condition-heavy code with lots of partially initialized variables. F# offers a number of elegant solutions in the form of discriminated unions and asynchronous computations. Table 1 compares explicit state using mutable variables and enum in C# to implicit state using async computations in F#, which are shorter.