[erlang-questions] Can I do the same with a fold easily
Jachym Holecek
freza@REDACTED
Sun Oct 18 21:55:45 CEST 2015
# Roelof Wobben 2015-10-18:
> I have this two functions :
>
> filter(List, Filter) ->
> lists:filter(Filter, List).
What is the point of this?
> split2(List) ->
> { filter(List, fun(X) -> math_functions:odd(X) end),
> filter(List, fun(X) -> math_functions:even(X) end) }.
>
> which produces both {[1,3],[2,4]}
>
> Could I this also do with a fold or is this the best solution.
Entirely untested, but the best is to 1) keep it real 2) keep it simple. :-)
foo(L) ->
foo(L, [], []).
foo([X | L], Es, Os) ->
case (X rem 2) of
0 ->
foo(L, [X | Es], Os);
1 ->
foo(L, Es, [X | Os])
end;
foo([], Es, Os) ->
{Es, Os}.
Add reverse/1 calls to the terminal branch if you particularly care about the
order of results.
BR,
-- Jachym
More information about the erlang-questions
mailing list