Erlang,谁能解释一下函数列表的行为?

13z8s7eq  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(173)

When I create a list like this:

LF1 =  [fun(X)->X*Y end || Y <- lists:seq(1,10)].

I receive

[#Fun<erl_eval.6.52032458>,#Fun<erl_eval.6.52032458>,
#Fun<erl_eval.6.52032458>,#Fun<erl_eval.6.52032458>,
#Fun<erl_eval.6.52032458>,#Fun<erl_eval.6.52032458>,
#Fun<erl_eval.6.52032458>,#Fun<erl_eval.6.52032458>,
#Fun<erl_eval.6.52032458>,#Fun<erl_eval.6.52032458>]

Then, when I use it like this:

[F(3) || F <- LF1].

I receive the result as it mentioned:

[3,6,9,12,15,18,21,24,27,30]

But, when I spawn all that functions from list:

LPF = [spawn(fun()->F(3) end) || F <- LF1].

[<0.1424.0>,<0.1425.0>,<0.1426.0>,<0.1427.0>,<0.1428.0>,
     <0.1429.0>,<0.1430.0>,<0.1431.0>,<0.1432.0>,<0.1433.0>]

And then send them the parameter:

[X ! 3 || X <- LPF].

I receive:

[3,3,3,3,3,3,3,3,3,3]

What is happening here?

zed5wv10

zed5wv101#

对不起,我发现我的错误:

[spawn(fun()->io:format("~p~n", [F(3)]) end) || F <- LF1].
3
6
9
12
15
18
21
24
27
30
[<0.1460.0>,<0.1461.0>,<0.1462.0>,<0.1463.0>,<0.1464.0>,
<0.1465.0>,<0.1466.0>,<0.1467.0>,<0.1468.0>,<0.1469.0>]

发送消息的方法不对。

相关问题