[erlang-questions] Human readable errors in lists module

Adam Lindberg <>
Tue Feb 10 16:22:24 CET 2009


Hi,

Is there any reason that for example lists:zipwith/3 returns a function clause instead of a human readable error when the lists are of different length? It might seem obvious at first but the reason I'm asking is because a colleague of mine just spent a long time debugging code with used list:zipwith/3 and it threw this error. What he did at first was to check that all arguments to lists:zipwith/3 was not zero (this is what the function clause error indicated).

lists:zipwith/3 could have been implemented as below (or something similar):

zipwith(F, [X | Xs], [Y | Ys]) -> [F(X, Y) | zipwith(F, Xs, Ys)];
zipwith(F, [], []) when is_function(F, 2) -> [].
zipwith(F, [], Ys) -> error(lists_of_different_length). %% Just a proposal, insert
zipwith(F, Xs, []) -> error(lists_of_different_length). %% preferred error mechanism here.

The function clause, noting the arguments as [#Fun..., [], [5,6,7,...]], is kind of misleading since it happens inside the lists:zipwith/3 function.

I can see the purists' argument here "that it is really a function clause" but I also see the pragmatist argument "that it is much easier to debug."

Cheers,
Adam



More information about the erlang-questions mailing list