Does a pure functional language loses its purity if global variables are allowed? I mean does having global variables affect the referential transparency of the language?
I suppose not, because of value semantics but I'm not sure and would like to know what other people think.
In a pure functional language, "variable" means something different than what it usually means in imperative languages. It is not variable in the sense that it can be reassigned within a given scope, but rather in the sense that each time it comes into scope, it may have a different value. But for the lifetime of that scope it remains constant. So for example, in the function
f x y = x + y
x
and y
are variables which become bound when f
is applied to them. Once bound, they never change within the scope of that invocation, they simply go out of scope at some point. Other invocations will bind x
and y
to different values. That is the sense in which functional variables "vary", which is closer (some might say identical) to the original mathematical meaning of a variable.
So, to your question: do global variables ruin purity? No, because global variables, since they never go out of scope, are effectively constants.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With