为什么这个 erlang 代码不起作用?

发布于 2024-08-10 16:56:33 字数 1058 浏览 4 评论 0原文

fib(N)->
       P1 = spawn(fun concFib:conFib/0),
       P2 = spawn(fun concFib:conFib/0),
       X=rpc(P1,N-2),Y=rpc(P2,N-1),X+Y.

conFib()->
       receive
               {Client,N} -> Client ! regfib(N)
       end.

rpc(Pid,Request)->
       case erlang:is_process_alive(Pid) of
               true -> begin
                                       Pid ! {self(),Request},
                                       receive
                                               {Pid,Respond} -> Respond
                                       end
                               end;
               false -> io:format("~w process is dead.",[Pid])
       end.


regfib(N)->
       case N<2 of
               true -> 1;
               false -> regfib(N,1,1,1)
       end.

regfib(N,N,X,_)-> X ;
regfib(N,M,X,Y)-> regfib(N,M+1,X+Y,X).

这个想法是将 fib(N) 过程分为两个过程,一个计算 fib(N-2),另一个进行计算。 fib(N-1)同时为fib(N)=fib(N-1)+fib(N-2)。当我运行前面的代码时,没有任何反应,光标停止在有限循环中或等待未到达结果。
plzzz 我需要帮助,我是一名新的 Erlang 程序员,提前致谢:)

fib(N)->
       P1 = spawn(fun concFib:conFib/0),
       P2 = spawn(fun concFib:conFib/0),
       X=rpc(P1,N-2),Y=rpc(P2,N-1),X+Y.

conFib()->
       receive
               {Client,N} -> Client ! regfib(N)
       end.

rpc(Pid,Request)->
       case erlang:is_process_alive(Pid) of
               true -> begin
                                       Pid ! {self(),Request},
                                       receive
                                               {Pid,Respond} -> Respond
                                       end
                               end;
               false -> io:format("~w process is dead.",[Pid])
       end.


regfib(N)->
       case N<2 of
               true -> 1;
               false -> regfib(N,1,1,1)
       end.

regfib(N,N,X,_)-> X ;
regfib(N,M,X,Y)-> regfib(N,M+1,X+Y,X).

The idea is to divide the fib(N) process into two process one calculates fib(N-2) and the other one calc. fib(N-1) concurrently as fib(N)=fib(N-1)+fib(N-2). when i run the previous code nothing happen and the cursor stop as in finite loop or waiting for not arriving result.
plzzz i need help i'm a new Erlang programmer,thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀念你的温柔 2024-08-17 16:56:33

在你的 conFib 中,你发送一个整数,但在 rpc 中等待一个元组。应该将其更改为:

conFib()->
       receive
               {Client,N} -> Client ! {self(), regfib(N)}
       end.

您可以通过在接收中使用 after 超时来避免这种情况。

In your conFib you send an integer, but await a tuple in rpc. Should change it to:

conFib()->
       receive
               {Client,N} -> Client ! {self(), regfib(N)}
       end.

You can evade such situations by using timeout with after in your receives.

过潦 2024-08-17 16:56:33

为了使计算并行,您可以执行以下操作:

fib(N)->
       P1 = spawn(fun test:conFib/0),
       P2 = spawn(fun test:conFib/0),
       P1 ! {self(), N - 2},
       P2 ! {self(), N - 1},
       receive
         {_, R1} -> R1
       end,
       receive
         {_, R2} -> R2
       end,
        R1 + R2.

重要的部分是在等待答案之前发送两个请求。
等待答案的部分当然可以做得更漂亮。

To make the computation parallel you could do something like:

fib(N)->
       P1 = spawn(fun test:conFib/0),
       P2 = spawn(fun test:conFib/0),
       P1 ! {self(), N - 2},
       P2 ! {self(), N - 1},
       receive
         {_, R1} -> R1
       end,
       receive
         {_, R2} -> R2
       end,
        R1 + R2.

The important part is that you send both of the requests before waiting for the answers.
The part waiting for the answers could of course be done in a more beautiful way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文