Erlang - all 方法
如果 Pred(Elem) 对 List 中的所有元素 Elem 返回 true,则返回 true,否则返回 false。
语法
all(Pred,lst)
参数
Pred − 将应用于字符串的谓词函数。
lst − 值列表。
返回值
如果 Pred(Elem) 对 List 中的所有元素 Elem 返回 true,则返回 true,否则返回 false。
例如
-module(helloworld). -import(lists,[all/2]). -export([start/0]). start() -> Lst1 = [1,2,3], Predicate = fun(E) -> E rem 2 == 0 end, Status = all(Predicate, Lst1), io:fwrite("~w~n",[Status]).
在上面的示例中,我们首先定义一个谓词函数,其中每个列表值都传递给匿名函数。 在该函数中,可以看到每个列表值是否能被 2 整除。
输出
当我们运行上面的程序时,我们将得到以下结果。
false