4 ms·
Quite practical I imagine, there is no reason to keep all the frames in memory. Keep the last n frames and get most of the benefits. The point of this programmi
by roenxi 3y ago
Quite practical I imagine, there is no reason to keep all the frames in memory. Keep the last n frames and get most of the benefits. The point of this programming style in a game is to have a few previous states when there is a crash/bug to figure out how the state ended up in a bad way. Maybe save a framesworth of state every keypress for debugging purposes. Then on a crash or bad state there is a debugging environment all ready to go, for the cost of a small amount of memory. Good deal.
Although in practice "purely functional" programming is a bit silly here. The point of PacMan is all the IO which is fundamentally all side effects. There are a lot of practical compromises that would be called "functional" like storing all the major state of the game in a big array (something like [turn1, turn2, turn3, ...]) and then implement a turnX -> turnX+1 animation function.
The fundamental insight of the functional approach is implementing a frame->frame function was already something that implicitly had to be done to make the game. If it is an explicit function then debugging it is easier. Because the input can be saved. No extra work is needed.
- taneq 3y ago> The point of PacMan is all the IO which is fundamentally all side effects. In fact, the point of a huge proportion of programming is IO and is "all side effects." It's still possible to adopt a functional style for internal calculations (and in fact this helps to decouple components and makes testing much easier, at the expense of relatively negligible copying overhead, so it's worth doing) but once you get beyond your internal toolkit and start actually trying to test the whole system, you're interacting with the outside world.
- epgui 3y agoFunctional programming is not some sort of mythical effect-free beast… All functional languages I know (including Haskell and Clojure) do side effects just fine, and there are plenty of different approaches for performing effectful computation sensibly within FP.
- sdenton4 3y agoCalling I/o side effects makes FP seem hopelessly academic. Everything having to do with the world is seemingly a corner case - the core of the programming philosophy is only concerned with the part where we push bits around uselessly. To be sure, I don't think this is a correct interpretation, but I do think it may play a role in the difficulty of getting people onboard with FP.
- hgsgm 3y agoA side effect is anything not modeled that can affect the computation. Input is an obvious example, as it's impossible to predict the value of an input. Output is less obvious, but there is uncertainty like whether the filesystem is full. The original Haskell style was to have a World object that was an input to every non-pure function.
- jibe 3y agoInput is an obvious example, as it's impossible to predict the value of an input. Do you mean this more abstractly or literally? In the example of PacMan, for every frame, the only possible input is nothing, left, right, up, or down. So you are be able to predict it is going to be one of those five, though not which one.
- mrkeen 3y ago> Do you mean this more abstractly or literally? >> Input is an obvious example, as it's impossible to predict the value of an input. > though not which one Literally.
- jibe 3y agoI’m asking as someone not familiar with function programming, so trying to learn, not be pedantic, btw! So because the possible input is not just a single value, it is not predictable. It doesn’t matter if the input was limited to a set of two values, or infinite, it is literally not predictable so therefore we don’t know the future state of the program?
- hgsgm 3y agoFor debugging, it's just as easy to save a copy of each frame state before modifying it, as it is to write in a functional style. Maybe easier.
- wyager 3y agoOnly if you exercise a high level of discipline in keeping all your state contained in the same place, having zero implicit state, and writing clonable data representations (i.e. no impredicative references, or some kind of serialization strategy that can deal with them). 99% of programmers do not write code this way when using imperative languages.
- c_crank 3y agoThe Java Enterprise style of over-abstraction that everyone hates does this, so plenty of imperative programmers will know of it and probably have implemented it to an extent.
- TillE 3y agoYou need to do this anyway if you're implementing saved games. Most modern PC games/engines have a really good instant quicksave system which dumps the entire world state very quickly.