[erlang-questions] Reassigning variables

Dale Harvey <>
Tue Mar 17 19:15:05 CET 2009


steve vinoski mentioned this solution in his reply to damien katz
finding the same problem.

http://steve.vinoski.net/blog/2008/03/10/damien-katz-criticizes-erlang/

f(X) ->
    lists:foldl(fun(F, Last) -> F(Last) end,
        foo(), [fun whee/1, fun bar/1, fun xyzzy/1]).

2009/3/17 Matthew Dempsky <>

> I like pattern matching in the majority of cases, but I find I write
> enough code where I need to incrementally update a data structure, and
> maintaining that code is a pain when I have code like:
>
>  X = foo(),
>  X1 = bar(X),
>  X2 = xyzzy(X1),
>  blah(X2).
>
> and later want to change it to:
>
>  X = foo(),
>  X1 = whee(X),
>  X2 = bar(X1),
>  X3 = xyzzy(X2),
>  blah(X3).
>
> This means having to change four lines of code, when really it's
> conceptually just one change.  I'd like to suggest being able to do
> something like:
>
>  X = foo(),
>  r(X) = whee(X),
>  r(X) = bar(X),
>  r(X) = xyzzy(X),
>  blah(X).
>
> where "r(?VAR) = ?EXPR" means to reassign ?VAR to ?EXPR, even if it
> previously has an assigned value.
>
> Thoughts?  Does anyone have suggestions for better syntax?  I think
> this can be handled with a parse transform, and I'm considering
> writing a parse transform to handle it.  (I haven't checked if anyone
> else has proposed similar functionality in the past, but this is
> something that's been bugging me for a while, and I've finally had to
> rename variables manually enough times to propose this.)
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090317/a3abf405/attachment.html>


More information about the erlang-questions mailing list