Reputation: 31
If I am not wrong, in erlang variables are to bind only once.
Like:
C = cat.
But take this as an example:
1> FallVelocity = fun(Distance) -> math:sqrt(2 * 9.8 * Distance) end.
#Fun<erl_eval.6.111823515>
And I can do:
3> FallVelocity(20).
19.79898987322333
4> FallVelocity(200).
62.609903369994115
5> FallVelocity(2000).
197.9898987322333
Why call a function many times over the course of a program. Doesn’t that mean the same variable will be bound many times?
Upvotes: 0
Views: 1336
Reputation: 1602
In the book "Introducing Erlang" by Simon St. Laurent there is a special section that speaks to your question:
Functions and Variable Scope
Erlang lets you bind a variable only once, but you might call a function many times over the course of a program. Doesn’t that mean the same variable will be bound many times? Yes, it will be bound many times but always in separate contexts. Erlang doesn’t consider multiple calls to the same function to be the same thing. It starts with a fresh set of unassigned variables every time you call that function.
Similarly, Erlang doesn’t worry if you use the same variable name in different functions or function clauses. They aren’t going to be called in the same context at the same time, so there isn’t a collision.
The place you need to avoid re-assigning values to an already bound variable is within a given path through a given function. As long as you don’t try to reuse a variable in a given context, you shouldn’t have to worry.
- Introducing Erlang, Chapter 2 - Functions and Modules, p.18
Upvotes: 4
Reputation: 107759
Rather than say that each variables are only bound once, it would be more precise to say that each each instance of a variable is only bound once. Each time the FallVelocity
function is called, this creates an instance of the Distance
variable, and this instance is bound to the value passed as a parameter in the call to FallVelocity
.
In Erlang, a given instance of a variable receives a value at the point when it is created, and there is no concept of assigning it a different value afterwards.
Most other languages have the same concept of instances of variables. When you call a function in an imperative language, this creates new instances of its local variables, and these instances disappear when the function returns. The difference between Erlang (and other functional languages) and imperative languages is that an instance of a variable in Erlang designates a value, whereas an instance of a variable in an imperative language designates a storage location, which itself contains a value that can change over time.
As for FallVelocity
, there is only one instance of it. The value of FallVelocity
is a function (it is not the result of the function; the value returned by the function, i.e. the result of the function, is not given a name in your snippet). If you call this function multiple times, the code of the function is executed each time — and it's still the same function that's executed.
Upvotes: 1
Reputation: 20245
FallVelocity refers to a function. Then you are calling FallVelocity a couple of times with different parameters. And of course, each different invocation of FallVelocity will yield a different result.
The result of each invocation of FallVelocity isn't assigned to FallVelocity var. The purpose of FallVelocity is to refer to a function, not to the result of the invocation of that function.
What you can't do is the following:
3> Hoohaa = FallVelocity(20). %% This is ok
19.79898987322333
4> Hoohaa = FallVelocity(200). %% Nope, it will fail
Line #4 will fails because Hoohaa Variable is already bounded to 19.79898987322333 (thanks to Patterns matching in Erlang).
Upvotes: 2