optimization of list comprehensions
Richard A. O'Keefe
ok@REDACTED
Tue Mar 7 22:41:43 CET 2006
Mats Cronqvist <mats.cronqvist@REDACTED> wrote:
foo(Xs) -> foo(Xs, root).
foo([], A) -> A;
foo([X|T], A) when is_atom(X) -> foo(T, {A,X});
foo([_|T], A) -> foo(T, A).
would look a lot better like this (using Per Gustafsson's syntax;
http://www.erlang.org/ml-archive/erlang-questions/200603/msg00034.html)
foo(Xs) ->
({A,X} || X<-Xs, A<--root, is_atom(X)).
Er, weren't round parentheses the proposed syntax for
"evaluate these expressions like a list comprehension but throw
the results away and just return 'ok'"?
If the normal order of binding in Erlang list comprehensions were
followed in this syntax, A would be rebound to 'root' every time.
<-- and <- are MUCH too similar; I defy anyone in the heat of battle
(manager has locked you into the building to finish code which MUST
ship tomorrow morning, yes that really happened to me) to see the
difference.
I am also extremely unconvinced by this example since it is a very very
strange thing to want to compute. If such a weird data structure
-type weird(X) -> root | {weird(X), X}.
were really needed, we'd *already* have functions
list_to_weird([]) -> root.
list_to_weird([X|Xs]) -> {list_to_weird(Xs), X}.
weird_to_list(root) -> [].
weird_to_list({Xs,X}) -> [X|weird_to_list(Xs)].
and the example would *really* be
foo(List) ->
list_to_weird([Atom || Atom <- list, is_atom(Atom)]).
i reject the argument "there is noo need for new syntax since
one can already do this";
foo(Xs) ->
lists:foldl(fun(X,A) when is_atom(X) -> {A,X}; (_,A) -> A end, root,Xs).
firstly; it is actually a lot more difficult to read (although the LOC is the
same).
But that is NOT the way you would do it using existing syntax.
You would do it like this:
foo(List) ->
list_to_weird([Atom || Atom <- list, is_atom(Atom)]).
and that's the clearest of all.
secondly; for whatever reason normal industry programmers(*)
will rarely if ever use lists:fold* (or indeed anything that involves
funs). they didn't use lists:map or lists:foreach either, but they do
use list comprehensions.
If you have evidence to back this up, it's publishable.
Please publish it. Some sort of survey analysing what kind of
programmers use what kind of language features would be most illuminating.
But if I understand you, you are claiming that normal industry programmers
are incompetent, ineducable, or both.
More information about the erlang-questions
mailing list