()的Erlang函数?

Ale*_*ith 1 erlang time for-loop

根据我的理解,Erlang没有能力进行for循环.但是在此代码段中,它使用函数for().我真的不明白这个()函数所以任何帮助都是适用的.

-module(helloworld). 
-export([max/1,start/0]). 

max(N) -> 
   Max = erlang:system_info(process_limit), 
   io:format("Maximum allowed processes:~p~n" ,[Max]), 

   statistics(runtime), 
   statistics(wall_clock), 

   L = for(1, N, fun() -> spawn(fun() -> wait() end) end), 
   {_, Time1} = statistics(runtime),
   {_, Time2} = statistics(wall_clock),
   lists:foreach(fun(Pid) -> Pid ! die end, L),

   U1 = Time1 * 1000 / N, 
   U2 = Time2 * 1000 / N, 
   io:format("Process spawn time=~p (~p) microseconds~n" , [U1, U2]).

wait() ->
   receive 
      die -> void 
   end. 

for(N, N, F) -> [F()]; 
for(I, N, F) -> [F()|for(I+1, N, F)]. 

start()->
   max(1000), 
   max(100000).
Run Code Online (Sandbox Code Playgroud)

另外Erlang的运行时和wall_clock的区别是什么?我相信wallclock是基于计算机时钟的,而运行时是基于Erlang中的某种刻度?我可能错了

7st*_*tud 5

我真的不明白这个()函数所以任何帮助都是适用的.

重命名函数xyz().现在,它有意义吗?

xyz(N, N, F) -> [F()]; 
xyz(I, N, F) -> [F()|xyz(I+1, N, F)].
Run Code Online (Sandbox Code Playgroud)

xyz()函数的第一个子句查找相同的第一个和第二个参数(N, N...).如果前两个参数相同,则xyz()返回一个包含调用第三个参数的返回值的列表.

当前两个参数不同时,xyz()函数的第二个子句将匹配(I, N, ...).在这种情况下,调用第三个参数,它的返回值是列表的头部,列表的尾部是对xyz()函数的递归调用,其中第一个参数递增.

那么,让我们尝试一个简单的例子:

-module(f1).
-compile(export_all).

show() ->
   hello.


xyz(End, End, F) -> [F()]; 
xyz(Start, End, F) -> [F()|xyz(Start+1, End, F)].

test() ->
    xyz(0, 5, fun show/0).
Run Code Online (Sandbox Code Playgroud)

在shell中:

5> c(f1).    
f1.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,f1}

6> f1:test().
[hello,hello,hello,hello,hello,hello]
Run Code Online (Sandbox Code Playgroud)

这是另一个例子:

for(End, End) ->
    io:format("~w~n", [End]); 
for(Start, End) -> 
    io:format("~w~n", [Start]),
    for(Start+1, End).


test() ->
    for(0, 5).
Run Code Online (Sandbox Code Playgroud)

在shell中:

12> c(f1).    
f1.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,f1}

13> f1:test().
0
1
2
3
4
5
ok
14> 
Run Code Online (Sandbox Code Playgroud)