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
5
u/mister_drgn Apr 22 '25
“Maybe” is a different monad from the one I was talking about. I know better than to try and explain monads in a quick reddit post, but if you’re really curious, I personally found this online book helpful for explaining haskell and monads: https://learnyouahaskell.com/chapters
I would again stress that the Haskell approach is only one option, and many would view it as the most extreme approach to purity in a functional language.
I believe one way to think of what Haskell achieves, and what you can do yourself in other languages, is that your code can have side effects, such as reading to and writing from a database of users, but all of those effects happen at the to top level in your code (think of it as function calls from your main() function). This isolates these effects from the rest of your code, so you don’t have to worry about/keep track of whether some random function call buried deep in your code is going to have a side effect that changes global state. This is considered desirable, as it makes your code easier to maintain and easier to test. You can follow this strategy in any language, but Haskell forces you to do it.