r/functionalprogramming • u/Level_Fennel8071 • Apr 21 '25
Question mutable concept confusion
hello,FP newcomer here, is this considered to be functional? if not how can we implement global state in any application.a good resources that explain this part is appreciated
// Pure function to "increment" the counter
const increment = (count) => count + 1;
// Pure function to "decrement" the counter
const decrement = (count) => count - 1;
// Pure function to "reset" the counter
const reset = () => 0;
// Example usage:
let count = 0;
count = increment(count); // count is now 1
count = increment(count); // count is now 2
count = decrement(count); // count is now 1
count = reset(); // count is now 0
10
Upvotes
View all comments
Show parent comment
6
u/mister_drgn Apr 22 '25 edited Apr 22 '25
The answer to that gets complicated. A few thoughts:
Learning about monads would likely take some time, but they are pretty interesting. Again, many functional languages don’t take things as far as Haskell and simply allow you to have mutable state when you absolutely need it.
You certainly don’t need mutable state for keeping track of a count, as in your little code snippet. Likely you would use recursion, but it depends on what you’re actually trying to do with the count.