Sometimes I miss destructively updating local variables

Dominic Williams <>
Fri Jul 7 08:39:39 CEST 2006


Hi James,

> What bugs me is having to list all the variables in the make_line_rec
> call.

How about using a dict (or orddict)?

make_line_rec([{Tag,Ofs,Len}|T], B, Acc, Dict)
  when Tag==x1; Tag==y1; Tag==x2; Tag==y2 ->
    make_line_rec(T, B, Acc, dict:store(Tag, parse_units(B, Ofs, Len),
Dict));
make_line_rec([{Tag,Ofs,Len}=Attr|T], B, Acc, Dict) ->
    case is_attr(Tag) of
	true  -> make_line_rec(T, B, [get_attr(Attr, B)|Acc], Dict);
	false -> make_line_rec(T, B, Acc, Dict)
    end;
make_line_rec([], _, Acc, Dict) ->
    {line, Acc,
     dict:fetch(x1, Dict), dict:fetch(y1, Dict),
     dict:fetch(x2, Dict), dict:fetch(y2, Dict)}.

That last clause could also be:

make_line_rec([], _, Acc, Dict) ->
    list_to_tuple([line, Acc | [dict:fetch(X, Dict) || X <- [x1, y1, x2,
y2]]]).

Cheers,

Dominic Williams
http://www.dominicwilliams.net

----



More information about the erlang-questions mailing list