Muzaaya Joshua
Muzaaya Joshua

Reputation: 7836

Re-using my variables in Erlang Code, not Erlang Shell

In the Erlang shell i can re-use my variables very well. like this:

1> R = "muzaaya".
"muzaaya"
2> f(R).
ok
3> R = "muzaaya2".
"muzaaya2"
So, i cannot call f(Variable) in my source code because i do not know which module this function belongs to. I have tried modules like: erlang,shell,c, e.t.c. Has anyone tried re-using variables in Erlang Source code, other than just in the Shell ? How did you do it ? Thanks

Upvotes: 0

Views: 575

Answers (4)

rvirding
rvirding

Reputation: 20926

As other have already pointed out f() is a shell command and only exists in the shell. That f(), and all other shell commands, looks like a normal function call is because the only way to do something in Erlang is to call a function. And the shell does not introduce any new syntax. All shell commands behave like normal functions in that they always return a value.

It was not deemed necessary to be able to use f() in normal functions, although there are many who disagree and find the once only binding of variables unnecessarily restrictive.

Upvotes: 2

Kijewski
Kijewski

Reputation: 26043

The REPL shell is interpreted, the code file is compiled. The shell comes in handy to test things, but you would not write your web server in a shell. ;-)

It would be possible and not even difficult for the Erlang hackers to implement an f(V) language construct, but it would not fit the Erlang design model. Mind, no function could accomplish the forgetting of a variable, so it had to be done in a new native language construct. When compiled, the virtual machine does not know the variables anymore, as Erlang is run by a rather ordinary stack machine, not much different from the JVM.

It just would not be functional programming if one could rebind a variable V.

Upvotes: 3

Lukas
Lukas

Reputation: 5327

The functions which are listed when you type help(). in the shell are shell only functions and cannot be used when programming Erlang. f() is one of there functions.

Upvotes: 2

Alexey Romanov
Alexey Romanov

Reputation: 170899

No, you can't do this inside a module.

Upvotes: 4

Related Questions