It's probably not the author trying to say, but at a higher level a game is just:<p>new_game_state = simulate_a_frame(old_game_state, user_input, time_delta)<p>So of course it's possible. Whether it's practical is another question.
A Quake clone written in Haskell [1] suggests that it is quite possible. That's just one of the games listed on [2], along with game engines and libraries.<p>[1] <a href="https://wiki.haskell.org/Frag" rel="nofollow noreferrer">https://wiki.haskell.org/Frag</a><p>[2] <a href="https://wiki.haskell.org/Applications_and_libraries/Games" rel="nofollow noreferrer">https://wiki.haskell.org/Applications_and_libraries/Games</a>
Totally possible. Here’s a link to a game recently released on Steam that is written in Haskell and whose source is open: <a href="https://github.com/incoherentsoftware/defect-process">https://github.com/incoherentsoftware/defect-process</a><p>I recently hacked together an asteroids clone in Haskell with SDL2 and not much else. It’s not super pretty but it works.<p>I’ve talked to folks who’ve been using the newer effects libraries taking advantage of the new delimited continuation primops in GHC 9.6 for their game dev. Even with very high level libraries their reporting acceptable performance.<p>With enough dedication I’m certain anyone could make whatever game they wanted in an FP language.
Functional core + imperative shell can describe any kind of problem.<p>The biggest problem with the approach is that mainstream functional languages put most effort in the functional core rather than the imperative shell modeling.<p>Effect systems (ZIO for Scala, effect-ts) are the only places where fibers allow composing, scheduling and managing "processes" in a functional way.
It is completely possible of course.<p>I think questions like this are really getting at, is how do you update the 'huge-game-state' without mutating it, changing variables.<p>People have a very ingrained feeling that the game state is updated by updating variables.<p>But in functional languages, the state is immutable, so if you are new to functional languages you ask 'man how do I update this'.<p>Then, if this new person takes next step and is told 'you make a copy of the game state', they are like 'man that must be a big performance hit to copy the whole thing'.<p>So I think questions like this are really about how to handle a state with immutable data. How to make a copy efficiently, like in parts, or whatever. And then of course, you are down the rabbit whole of changing your brain thinking towards functional style.
If there was one single thing I learned from my functional programming class in college, it’s that there exists a way to implement imperative programming in a pure functional language. I distinctly remember the aha that Turing complete means Turing complete, and functional vs imperative is a style that can be emulated in any Turing complete language. Now for the life of me I can’t remember what the exact trick or mechanism was, does anyone know what I’m talking about? I’m pretty sure it leveraged one of the named functional constructs, similar to a thunk or y-combinator or something. I’m old enough that use of “monad” wasn’t much of a thing at the time.
Fable [1] (F# to JS compiler) has pac-man game as one of samples [2]<p>[1] <a href="https://fable.io/" rel="nofollow noreferrer">https://fable.io/</a><p>[2] <a href="https://github.com/fable-compiler/repl/blob/master/public/samples/games/pacman.fs">https://github.com/fable-compiler/repl/blob/master/public/sa...</a>
Hey, it's my time to shine! I rewrote Chips Challenge in Haskell years ago:
<a href="https://github.com/egonSchiele/chips">https://github.com/egonSchiele/chips</a><p>It's not <i>better</i> than using C, but worked just fine when I wrote it in 2014. I'd say, unless you're planning to sell it, write the game in whatever language you enjoy.
I like this. He shows some of the basic ideas of functional reactive programming and of entity component systems. And then the thing about large objects not being efficient to update, well it's not efficient in C either, or in databases or in anything really. Understanding how data transforms and the dependencies between data is usually A Good Thing.
In Conrad Barski's book, Land of Lisp, you are taught to do so.
Check out the website: <a href="http://landoflisp.com" rel="nofollow noreferrer">http://landoflisp.com</a>
In university I had a mind bending experience writing snake in purely functional javascript, I was so proud of it and showed my friends and the reaction was “oh god”<p>I can’t say they were wrong
Just to pitch in with a Clojurescript/Quill example, where the game loop takes a function that takes game state, modifies it, and returns it for the next iteration of the game loop.<p><a href="https://github.com/opyate/minild72/blob/master/src/pong/core.cljs#L59">https://github.com/opyate/minild72/blob/master/src/pong/core...</a>
I've tried this a lot, and I will say no, it's not realistically possible if you're being a purist.<p>The simple counterexample, take setpixel, which takes an image buffer, and returns an image buffer with one pixel changed. Now imagine having a large bitmap and using setpixel to draw a sprite to it. For every non-transparent pixel in the sprite you need a whole copy of the whole buffer. Obvs there are lots of improvements and cleverness but compared to the speed and simplicity of an imperative code just modifying an image buffer in-place it's vastly slower, more complicated or both.<p>However you can manage to write "most" of your game in a functional way and if you're careful about it, and use tricks like copy-on-write to make some things act functional, you can do it. Writing your game in a functional mindset, and keeping data immutable when you can has a lot of benefits.
There's Monadius, a clone of Gradius written in Haskell: <a href="https://github.com/tanakh/monadius">https://github.com/tanakh/monadius</a>
Some Haskell pacmans:<p>- <a href="https://github.com/EgorDm/fp-pacman">https://github.com/EgorDm/fp-pacman</a> (<a href="https://www.youtube.com/watch?v=DlifcJ4cexw">https://www.youtube.com/watch?v=DlifcJ4cexw</a>)<p>- <a href="https://github.com/Jcklly/PacMan">https://github.com/Jcklly/PacMan</a><p>- <a href="https://github.com/ajkavanagh/pacman">https://github.com/ajkavanagh/pacman</a><p>and other games:<p>- <a href="https://wiki.haskell.org/Applications_and_libraries/Games" rel="nofollow noreferrer">https://wiki.haskell.org/Applications_and_libraries/Games</a><p>See also the #haskell-game chat on Matrix and Libera IRC.
I think the most fun and behinner-approachable way to build games with functional programming is with Elm [1].<p>See a few (small, demo) games built by the community in [2] .<p>Notice Elm has abandoned the FRP approach in favor of Model-View-Update [3].<p>[1] <a href="https://elm-lang.org/" rel="nofollow noreferrer">https://elm-lang.org/</a>
[2] <a href="https://github.com/rofrol/elm-games">https://github.com/rofrol/elm-games</a>
[3] <a href="https://elm-lang.org/news/farewell-to-frp" rel="nofollow noreferrer">https://elm-lang.org/news/farewell-to-frp</a>
Part of me questions the old view that functional programming isn’t performant.<p>If you think about it, 1.) most performance comes down to memory access. 2.) It is widely known that accessing memory off the stack is faster than allocating memory on the heap. 3.) calling a function pushes everything onto the stack, starts a new stack pointer, and when it’s done releases everything from the stack.<p>So my thinking is if by definition functional programming is about organizing your program as a pure composition of functions, doesn’t that also imply an ideal memory access pattern? Can someone tell me where I'm wrong?<p>I guess the one issue where this comes into doubt is with strings of unknown length, and other data types that must be heap allocated, but even in those cases it seems that problem would equally affect all other paradigms.
It is both possible and relatively easy. I wrote a game similar to Pac-Man using Elixir, only I used characters from Firefly instead. I used it in several job interviews to prove I can code.
A game as tiny as pacman is trivial to make run fast enough even with the most naive functional style like rebuilding the entire game state every frame.
… would it be presumptive of me to think that there are <i>no</i> games which could not be written in a functional language?<p>I mean like… what’s stopping you?