r/functionalprogramming • • 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

22 comments sorted by

View all comments

Show parent comment

3

u/WittyStick Apr 23 '25

It's in other languages, not only clean. Usually shadowing will only shadow variables in outer scopes from the current one (as in Javascript). The syntax using # in Clean allows shadowing the variable in the same function. Without this special syntax you would have to give new names each time - count0, count1, etc.

In other functional languages like Haskell or ML, let introduces a new scope, so shadowing will happen if you have.

let count = increment(count) in
let count = increment(count) in  -- this new count shadows the one above.
count

3

u/Level_Fennel8071 Apr 23 '25 edited Apr 23 '25

ah, if that what you mean by shadowing, then you are completely right that shadowing not equal mutation, but in the example above AI was trying to convince me its not mutation to reassign to variable in the same scope