It seems you have to call f() to 'forget' a variable before assigning a new value to it - what a maddening name for a built-in function ;-)
It seems you have to call f() to 'forget' a variable before assigning a new value to it - what a maddening name for a built-in function ;-)
Rather than deal with the complexities of synchronization, some languages opt to make all their data immutable and encourage the programmer to write code in a functional style. The fact that the f() function seems unintuitive is because that's not how code is written in Erlang. f() is merely a convenience function for unbinding a value from the Erlang shell, and it is not used in the real world. Rather, you setup initial bindings and then use recursive functions to yield new values as needed.
This requires a shift in your thinking about programming, but it offers huge advantages when working within a concurrent model. For more info on the dangers of mutable state in concurrent systems, I suggest the following article:
Also, the immutable variable have nothing to do with concurrency. Since threads cant touch each others data, and they can only talk to each other through messages, immutability only applies to a local scope, where its really no big deal.
Last time I checked somebody was making a scripting language built on top of the erlang runtime and he says that immutable variables are nonsense.
At this point you might be wondering how it’s possible to program with- out variables. How can you express something like X = X + 1 in Erlang? The answer is easy. Invent a new variable whose name hasn’t been used before (say X1), and write X1 = X + 1.
X=#todo{}.
X1 = #todo{status=urgent, text="Fix errata in book"}.
X2 = X1#todo{status=done}.
From Armstrongs thesis X = 5; X1 = X + 10;
I found those just by searching on "X1". There are more X1 examples in the erlang otp sources. Who knows what else is in there.It does, but you'll rarely see this pattern in any real Erlang code base. And when you see it, it will not be "littered". It's an exception, not an idiom.
And those samples from Joe's book and thesis are just snippets with no context. BTW, O'Reilly's "Erlang Programming" doesn't even provide such an example.
Edit: One example when I do use this pattern is constructing proplists for some function calls.
Or, rebinding variables is generally not the clearest way to express the intent of your calculation. Abstractions like fold are more pure descriptions of the desired calculation.
I do agree with the consensus that the clojure recur, expresses something about intent that is missing from Erlang.
I enjoy functional depictions of algorithms, but there are many programmers who are currently more comfortable with imperative code.
The guy you're looking for is Tony Arcieri, and his language is Reia. And here's the FAQ about assignments:
http://wiki.reia-lang.org/wiki/FAQ#Destructive_assignment.3F...
f(..., State0) ->
{A,State1} = foo(..., State0),
{B,State2} = bar(..., State1),
{C,State3} = baz(..., State2),
Res = fubar(A, B, C, ...),
{Res,State3}.
Otherwise you just create confusion.In Prolog, it's because there's only unification, not assignment. Semantically, it's as if "X always had that value, it just wasn't known until now" - you can pass around unbound values and retroactively bind them for all places of use, but you can't ever change them. (Difference lists are probably the best example of this in action.)
Having mutable variables is definitely possible but I don't really see the problems with immutable ones. You get used to it very quickly.
What works for me is thinking of fp as describing mathematical relationships instead of changing state