简体   繁体   English

Erlang函数for()?

[英]Erlang function for()?

From my understanding Erlang doesn't have the capabilities to do a for loop. 据我了解,Erlang没有执行for循环的功能。 However in this code snippet it uses the function for() in it. 但是,在此代码段中,它使用了函数for()。 I don't really understand this for() function so any help is apprechiated. 我不太了解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).

Also what's the difference in run time and wall_clock in Erlang? 另外,Erlang的运行时间和wall_clock有什么区别? I believe that wallclock is based off computers clock while runtime is based off of some sort of ticks in Erlang? 我相信挂钟是基于计算机时钟的,而运行时是基于Erlang中的某种滴答声的? I may be wrong though 我可能是错的

I don't really understand this for() function so any help is apprechiated. 我不太了解for()函数,因此不胜感激。

Rename the function xyz(). 重命名函数xyz()。 Now, does it make sense? 现在,这有意义吗?

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

The first clause of the xyz() function looks for a 1st and 2nd argument that are identical (N, N...) . xyz()函数的第一个子句将查找相同(N, N...)的第一个和第二个参数。 If the first two arguments are identical, then xyz() returns a list containing the return value of calling the 3rd argument. 如果前两个参数相同,则xyz()返回一个列表,其中包含调用第3个参数的返回值。

The second clause of the xyz() function will match when the first two arguments are different (I, N, ...) . 当前两个参数不同(I, N, ...)时,xyz()函数的第二个子句将匹配。 In that case, the third argument is called and it's return value is the head of a list, with the tail of the list being a recursive call to the xyz() function, where the first argument is incremented. 在这种情况下,将调用第三个参数,并且其返回值是列表的开头,列表的xyz()是对xyz()函数的递归调用,其中第一个参数递增。

So, let's try a simple example: 因此,让我们尝试一个简单的示例:

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

In the 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]

And here is another example: 这是另一个示例:

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


test() ->
    for(0, 5).

In the 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM