Sometimes I miss destructively updating local variables

James Hague james.hague@REDACTED
Thu Jul 20 16:57:58 CEST 2006


Thanks for all the interesting replies to this.  I gave them all some
thought and continued to mull over the problem.  What it comes down to
for me is that Erlang/BEAM handles destructive updates extraordinarily
well behind the scenes.  Records and dicts are fine, but they bypass
the beauty of the current system.  Just list all variables in a
function, and update them as needed in a tail call.  I think lots of
people don't realize that in a function like this:

f(A,B,C,D,E,F,G,H,I,J) ->
   f(A,B,C,D,E,F+1,G,H,I,J);
[...]

essentially turns into this code:

f:
   parameter(5) += 1
   goto f

What would be nice is to have a syntax similar to that of records,
where one lists the changes to the parameter list.  Let's say that "@"
means "tail call," so in this example we'd have:

f(A,B,C,D,E,F,G,H,I,J) ->
   @(F -> F + 1);

Ignoring the syntactic details, I really like this.  It's simple,
clean, and leads to code that's very tight in terms of source and
object code.  It's also easy to write a short Perl script to take a
function in the above format and expand the "@" syntax.  (It's a
little trickier than it needs to be, because parameters like [H|T]
need to look like [H|T]=L, but this wouldn't be needed with native
support.)



More information about the erlang-questions mailing list