gpt4 book ai didi

erlang - 产卵有什么问题吗?

转载 作者:行者123 更新时间:2023-12-02 09:17:16 25 4
gpt4 key购买 nike

我在Erlang的文档中发现函数spawn的格式如下:spawn(Module, Name, Args) -> pid() 。我试过了。但这不起作用。出了什么问题?

代码:

-module(tut).

-export([main/0]).


main() ->
spawner(),
spawner(),
spawner().

for(Counter) when Counter == 0 ->
io:fwrite("0");
for(Counter) when Counter > 0 ->
io:fwrite("~p\n", [Counter]),
for(Counter -1).

spawner() ->
spawn(tut, for, [50]).

控制台输出:

68> c(tut).    
tut.erl:12: Warning: function for/1 is unused
{ok,tut}
69> tut:main().
<0.294.0>
=ERROR REPORT==== 6-Sep-2017::15:06:29 ===
Error in process <0.292.0> with exit value:
{undef,[{tut,for,"2",[]}]}
70>
=ERROR REPORT==== 6-Sep-2017::15:06:29 ===
Error in process <0.293.0> with exit value:
{undef,[{tut,for,"2",[]}]}

=ERROR REPORT==== 6-Sep-2017::15:06:29 ===
Error in process <0.294.0> with exit value:
{undef,[{tut,for,"2",[]}]}

最佳答案

spawn 的三参数版本仅在您调用的函数被导出时才有效。要使其正常工作,除了 main 之外,您还可以导出 for 函数,或者使用 spawn 的单参数版本,传递进行本地调用的匿名函数(“有趣”),从而绕过导出函数的需要:

spawn(fun() -> for(50) end)

关于erlang - 产卵有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45740256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com