我想解决的问题是
Write a split(L) which returns {Even, Odd}.
我看到的可用代码
-export([even/1, odd/1, filter/2, split_using_filter/1]).
even(Integer) -> Integer rem 2 =:= 0.
odd(Integer) -> not even(Integer).
filter(F, L) -> [T || T <- L, F(T) =:= true].
split_using_filter(L) -> Even = filter(fun(X) -> X band 1 == 0 end, L),
Odd = filter(fun(X) -> X band 1 == 1 end, L),
{Even, Odd}.
我现在正在做的是传递
fun(X) -> X band 1 == 0 end
作为even
函数,类似于odd
问题
有没有办法将even(Integer)
函数作为参数传递给filter
?而不是重写逻辑?
感谢
答案 0 :(得分:3)
你的代码在这里:
split_using_filter(L) -> Even = filter(fun(X) -> X band 1 == 0 end, L),
Odd = filter(fun(X) -> X band 1 == 1 end, L),
{Even, Odd}.
你的意思是你想要在???
之后做split_using_filter(L) ->
Even = filter(fun even/1, L),
Odd = filter(fun odd/1, L),
{Even, Odd}.
答案 1 :(得分:1)
Even = fun(X) -> X rem 2 =:= 0 end.
Odd = fun(X) -> X rem 2 /= 0 end.
filter(F, L) -> [T || T <- L, F(T) =:= true].
split_using_filter(L) ->
{filter(Even, L), filter(Odd, L)}.