My name is Luke Venediger, I'm the author of Erlang for Skeptics. Thanks for visiting my book and for the comments. It's very much a work in progress and has a long way to go, but I'm excited about getting the community involved early on. I'm working on a public feedback mechanism, but for now please post feedback below this comment.
I've been using Google Wave to build the plot and outline, together with my friends TheColonial (http://buffered.io/) and StackingIt (http://www.blog.stackingit.com/). Wave rocks as a real-time review system, and has definitely improved the quality of my book.
I'll have a more substantial website up in the next week or so that will have a comments section and an RSS feed for updates. In the mean time you can follow @erlangforskeptics or myself @lukevenediger on twitter.
The rationale behind immutable (a.k.a. unchangeable) variables is rooted in Erlang being designed as a concurrent functional programming language from the bottom up. Whenever you have multiple operations manipulating a single value, you're immediately responsible for synchronizing access to that value and ensuring it always exists in a consistent state.
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:
While I fully agree that the immutability-by-default mindset helps with concurrency, having single-assignment variables within the local scope of functions (what zephj is complaining about) is strictly due to Erlang being originally implemented in Prolog. Strictly speaking, letting you destructively modify local variables in Erlang wouldn't have any impact on concurrency.
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.)
Ok, I didn't make the distinction regarding local function scope. Clojure distinguishes these types of values as "transients", and yes, they definitely provide value in regard to both convenience and performance.
I've done some programming in Erlang and I found the immutable variables to be a total pain in the ass that gave no benefit. Your code ends up littered with var=1 var2=var+1 type idioms.
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.
If your code ends up littered with Var2 = Var1 + 1 type "idioms", then you are probably doing it wrong. I hardly ever write code like that (or see other's Erlang code like that).
It's by no means an idiom.
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.
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.
Erlang's style was very strongly influenced by Prolog, and in Prolog code, it's quite common to see e.g. "X1 = X + 1", but having X2, X3, etc. as well is rare - there's usually not a succession of modified versions of a variable in the same function (clause), because it's a sign that you could break things up better. The same is true in Erlang - code with several such variables is written with a strong Java accent (or whatever), not in idiomatic Erlang.
You can say the same thing another way. If your code is littered with V2 = V1 + 1 "idioms", then you are coding in a way not well supported by Erlang.
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 only time it is sensible to do this with variable names is when they are some form of logical progression, for example when threading a state variable through the code which may be modified.
Variable bindings in Erlang are immutable to support the pattern matching. If variable bindings were not immutable, then Erlang would need syntax to distinguish between free and fixed variables in a pattern. The "=" in Erlang is pattern match, not assignment. Erlang could make bindings mutable without impacting the concurrent nature of Erlang because values would still be immutable.
It is as you say, you must make a choice, trying to have both becomes very confusing. In LFE (Lisp Flavoured Erlang) I went the other and added proper scoping. It is the lack of scoping which I consider to be the most confusing and has caused some strange features.
Having mutable variables is definitely possible but I don't really see the problems with immutable ones. You get used to it very quickly.
It nevertheless is confusing for somebody who doesn't know erlang. Even prolog uses different symbols for matching a variable and for defining a clause.
f() is only available in the Erlang shell/REPL. The reason it has such a short name is that it's intended for use in the shell and not in real code. Its purpose is for forgetting a variable name that you may have defined earlier during interactive testing in the shell. It's not available in compiled code and is not intended for re-assigning variables within a function.
The code listing are "Program Listing" sections in Lyx (http://www.lyx.org). Unfortunately Elyxer (http://www.nongnu.org/elyxer/), the Lyx->HTML converter, doesn't yet understand these sections and so skips them. I'm going to patch it to allow for these, and I'll also provide a PDF build next to the HTML export.
In my first (now trashed) version of the book I had started writing an introduction, but it got long-winded and verbose because I didn't know what I was introducing! Better to add the introduction in at the end, as you've mentioned :)
My name is Luke Venediger, I'm the author of Erlang for Skeptics. Thanks for visiting my book and for the comments. It's very much a work in progress and has a long way to go, but I'm excited about getting the community involved early on. I'm working on a public feedback mechanism, but for now please post feedback below this comment.
I've been using Google Wave to build the plot and outline, together with my friends TheColonial (http://buffered.io/) and StackingIt (http://www.blog.stackingit.com/). Wave rocks as a real-time review system, and has definitely improved the quality of my book.
I'll have a more substantial website up in the next week or so that will have a comments section and an RSS feed for updates. In the mean time you can follow @erlangforskeptics or myself @lukevenediger on twitter.
Thanks again, Luke Venediger