如何在模块内将函数作为参数传递?

How to pass functions as arguments inside modules?

我想解决的问题是

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?而不是重写逻辑?
谢谢

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)}.

你可以阅读这个http://learnyousomeerlang.com/higher-order-functions

您的代码在这里:

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}.