On the face of it, speaking as an old school "normal" programming guy since ALGOL to C and C++, the whole idea of FP is barking mad. I mean really, my programs are made of functions/methods, if I cannot mutate state globally or in some object how on Earth is my program supposed to make any progress and do anything useful?
The FP guys say we can't do that. They say that every function should produce the same output for a given input no matter how many times it is called. That it cannot mutate state outside of itself.
So we might think an FP game becomes:
new_game_state = do_game_step(old_game_state, user_input);
Which, as far as I can tell it does. That is of course nuts, it will involve a lot of copying of game state, waste a huge pile of memory and be very slow. Right?
Except, those FP guys are cunning. They cheat!
They say "What if
new_game_state is only slightly different than `old_game_sate'"?. And "What if instead of copying all of new to old we arrange that only the differences are noted, such that someone with a handle to new_game_state sees the changes but someone with handle on old game_state does not?
That idea seems to be quite doable when game_state is a complex mess of data in
trees, as it likely is. For example the same linked list will look different to someone with a pointer to
head as it does to someone with a pointer into the middle of the list, that they think is
head. See steffahn's tree example above.
This FP fraud became clear to me recently after watching:
"Monoids, Monads, and Applicative Functors: Repeated Software Patterns - David Sankel - CppCon 2020": https://www.youtube.com/watch?v=giWCdQ7fnQU
"Postmodern immutable data structures - Juan Pedro Bolivar Puente [C++ on Sea 2019]": https://www.youtube.com/watch?v=y_m0ce1rzRI
Impressive stuff. Sounds like a useful idea to have in ones tool kit. Although my old brain is still not sure how or where.
My apologies to anyone sensitive for my use of the words "cheat" and "fraud". They are there only for dramatic effect. In much the same way I might say the "structured programming" guys who banned
goto then had to cheat by replacing
continue, exceptions and recently generators.