[erlang-questions] Variables and side-effects
Ulf Wiger
ulf@REDACTED
Sun Dec 30 16:49:01 CET 2012
On 30 Dec 2012, at 12:16, Tyron Zerafa wrote:
> I have been reading about Erlang and I am finding a problem grasping the meaning of some terms related to variables.
>
> What do global, free and unbound variables stand for in Erlang? Do they mean the same thing?
Global variables is what Erlang doesn't have. ;-)
You can simulate global variables using processes or (within the scope of a process) using the process dictionary, as Michael Turner pointed out.
> Is there a notion of global/free/unbound variables in Erlang? For instance, can I somehow implement this lambda: λy -> x+y+1 ?
You can do that, by passing X to the function that creates the lambda:
lambda(X) ->
fun(Y) ->
X + Y + 1
end.
The closure will include X in its environment. Of course X will be immutable.
> If Erlang does not support global/ free/unbound variables, can we say that a function does not have side-effects?
You can't say that generally, since you can call side-effecting functions from any function. There is currently no way to guarantee that a function is pure, either through static or dynamic analysis.
OTOH, the number of side-effecting operations in Erlang are relatively few, so it's not too hard to determine that a function is pure in most cases. Note that any function that calls other modules will have to be treated as potentially side-effecting, since the semantics of the remote call is determined at the time of the call, and dynamic code loading can completely alter the type signature and semantics of an exported function.
BTW, variables are free until bound, but you cannot reference an unbound variable unless that expression also binds a value to it. In other words, an unbound variable cannot appear in the RHS of an expression. Thus, you cannot get a null reference exception in Erlang, which even Sir Anthony Hoare will agree is a Very Good Thing. [1]
BR,
Ulf W
[1] http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare
Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
http://feuerlabs.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20121230/82223ce2/attachment.htm>
More information about the erlang-questions
mailing list