Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global variables in pure functional languages [closed]

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.

like image 650
Jim Goodall Avatar asked Dec 24 '13 05:12

Jim Goodall


1 Answers

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.

like image 164
Tom Crockett Avatar answered Sep 21 '22 00:09

Tom Crockett