[erlang-questions] How to "chain" filters?

Per Melin <>
Tue May 20 12:05:03 CEST 2008


2008/5/20 Steve Davis <>:
> check ([H | T], A) ->
>     C = [{H, X, H / X} || X <- T, H rem 2 =:= 0, (X + H) rem 3 =:= 0],
>     check(T, [C | A]);
> check([], A) -> lists:flatten(lists:reverse(A)).

In the above you iterate over T when H is odd and no cases apply. This
should be more efficient:

check([H | T], A) when H rem 2 =:= 0 ->
    C = [{H, X, H / X} || X <- T, (X + H) rem 3 =:= 0],
    check(T, [C | A]);
check([_ | T], A) -> check(T, A);
check([], A) -> lists:flatten(lists:reverse(A)).



More information about the erlang-questions mailing list