# Writing Video Games in a Functional Style

DevFeed: [Writing Video Games in a Functional Style](<https://devfeed.tech/articles/writing-video-games-in-a-functional-style-21081.md>)

Original publisher: [Read original article](<http://prog21.dadgum.com/228.html>)

Author: James Hague

Published: 2016-12-29T06:00:00Z

Content type: article

Language: en

Sources: [James Hague](<https://devfeed.tech/sources/james-hague.md>)

Topics: [Functional programming](<https://devfeed.tech/topics/functional-programming.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Data structures](<https://devfeed.tech/topics/data-structures.md>), [Code](<https://devfeed.tech/topics/code.md>), [Erlang](<https://devfeed.tech/topics/erlang.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [functional](<https://devfeed.tech/tags/functional.md>), [functional-programming](<https://devfeed.tech/tags/functional-programming.md>), [games](<https://devfeed.tech/tags/games.md>), [immutability](<https://devfeed.tech/tags/immutability.md>)

## AI overview

The article examines whether video games and other interactive programs can be written in a functional style. It discusses the difficulty of preserving immutability in large, stateful, evolving programs and describes an approach based on function calls and immutable data structures, informed by experience rewriting a 60fps 2D shooter in mostly pure Erlang.

## Source excerpt

When I started this blog in 2007, a running theme was "Can interactive experiences like video games be written in a functional style?" These are programs heavily based around mutable state. They evolve, often drastically, during development, so there isn't a perfect up-front design to architect around. These were issues curiously avoided by the functional programming proponents of the 1980s and 1990s. It's still not given much attention in 2016 in either. I regularly see excited tutorials about mapping and folding and closures and immutable variables, and even JavaScript has these things now, but there's a next step that's rarely discussed and much more difficult: how to keep the benefits of immutability in large and messy programs that could gain the most from functional solutions--like video games. Before getting to that, here are the more skeptical functional programming articles I wrote, so it doesn't look like I'm a raving advocate: Admitting that Functional Programming Can Be Awkward Back to the Basics of Functional Programming Functional Programming Went Mainstream Years Ago Puzzle Languages Let's Take a Trivial Problem and Make it Hard Functional Programming Doesn't Work (and what to do about it) I took a straightforward, arguably naive, approach to interactive functional programs: no monads (because I didn't understand them), no functional-reactive programming (ditto, plus all implementations had severe performance problems), and instead worked with the basic toolkit of function calls and immutable data structures. It's completely possible to write a video game (mostly) in that style, but it's not a commonly taught methodology. "Purely Functional Retrogames" has most of the key lessons, but I added some additional techniques later: Purely Functional Retrogames (4 parts) Turning Your Code Inside Out A Worst Case for Functional Programming? Messy Structs/Classes in a Functional Style Reconsidering Functional Programming The bulk of my experience came from rew