If statement If..else If语句在receive..end语句中?

If statement If..else If语句在receive..end语句中?,if-statement,process,erlang,If Statement,Process,Erlang,我试图在Erlang中的receive..end.语句中创建一个if..else if条件,以便传递两个变量a和B,以便测试它们的等效性 在shell中,我尝试键入: 6> Pid = spawn(ifelse,receiving,[]). ** exception error: no match of right hand side value <0.54.0> 7> 顺便问一下,如果我有receiving(A,B)而不是两个变量,我将如何生成类似于Pid=spaw

我试图在Erlang中的
receive..end.
语句中创建一个
if..else if
条件,以便传递两个变量a和B,以便测试它们的等效性

在shell中,我尝试键入:

6> Pid = spawn(ifelse,receiving,[]).
** exception error: no match of right hand side value <0.54.0>
7> 

顺便问一下,如果我有
receiving(A,B)
而不是两个变量,我将如何生成类似于
Pid=spawn(ifelse,receiving,[])的变量。
?我尝试使用
Pid=spawn(ifelse,receiving(1,2),[])。
但是出现了一个错误。

如果您在文件中定义了函数receiving/2,这意味着您有类似于:

-module(ifelse).
-export([receiving/0,receiving/2]).

receiving() ->
    some_code.

receiving(A,B) ->
    other_code.
你可以称之为

Pid=spawn(如果是,则接收[1,2])

顺便说一下,在erlang中编写if语句是不常见的,原因是如果一个case与任何条件都不匹配,代码就会崩溃

5> F=fun(X) -> if (X rem 2) == 0 -> X+1 end end.
#Fun<erl_eval.6.82930912>
6> F(4).
5
7> F(5).
** exception error: no true branch found when evaluating an if expression
8> 
我在shell中对此进行了测试:

14> F = fun() ->                                        
14>     receive                                         
14>         {Pid,_A,_A} when is_pid(Pid) ->             
14>                 io:format(" B equals A ~n"),        
14>                 Pid ! "True";                       
14>         {Pid,_,_} when is_pid(Pid) ->               
14>                 io:format(" B does not equal A ~n"),
14>                 Pid ! "False";                      
14>         _ -> {error, wrong_value}                   
14>     end                                             
14> end.                                                
#Fun<erl_eval.20.82930912>
15> Pid = spawn(F).
<0.58.0>
16> Pid ! {self(),1,2}.
 B does not equal A 
{<0.51.0>,1,2}
17> % the returm value of "proc ! Mess" is Mess. It is what we get on the console on previous line
17> flush(). % use flush() to get the messages received by the shell
Shell got "False"
ok
18> Pid ! {self(),test,test}. % the process Pid is terminated now. when we send a message to it, 
18> % it is simply "lost".
{<0.51.0>,test,test}
19> % it is necessary though to use a new variable Pid1 and spawn a new process 
19> % (see rvirding message and user601836 answer)
19> Pid1 = spawn(F).         
<0.63.0>
20> Pid1 ! {self(),test,test}.
 B equals A 
{<0.51.0>,test,test}
21> flush().
Shell got "True"
ok
22> Pid2 = spawn(F).          
<0.68.0>
23> Pid2 ! {hello,test,test}. 
{hello,test,test}
24> flush(). 
ok
25> % of course there is no message sent back, no io:format to print something on the console,
25> % the returned value of the function in the error case is "lost".
25> % if you want to have a permanent process you must have a recursive loop, 
25> % calling receiving() were needed.
14>F=fun()->
14> 接收
14> {Pid,_A,_A}什么时候是_Pid(Pid)->
14> io:格式(“B等于A~n”),
14> Pid!“真实”;
14> {Pid,{,}什么时候是{Pid(Pid)->
14> io:格式(“B不等于A~n”),
14> Pid!“假”;
14> _u->{错误,错误的_值}
14> 结束
14> 结束。
#乐趣
15> Pid=繁殖(F)。
16> Pid!{self(),1,2}。
B不等于A
{,1,2}
17> %proc!Mess的returm值为Mess。这是我们在上一行的控制台上得到的
17> 刷新()。%使用flush()获取shell接收的消息
壳牌得到了“假”
好啊
18> Pid!{self(),test,test}.%进程Pid现在终止。当我们向它发送消息时,
18> 它只是“丢失了”。
{,测试,测试}
19> %但有必要使用新变量Pid1并生成新进程
19> %(请参阅rvirding消息和user601836应答)
19> Pid1=繁殖(F)。
20> Pid1!{self(),test,test}。
B等于A
{,测试,测试}
21>刷新()。
Shell说的是“真的”
好啊
22>Pid2=繁殖(F)。
23>Pid2!{你好,测试,测试}。
{你好,测试,测试}
24>刷新()。
好啊
25>%当然没有发送回消息,没有io:format在控制台上打印内容,
25>%错误情况下函数的返回值为“丢失”。
25>%如果要有一个永久进程,必须有一个递归循环,
25>%需要调用receiving()。

如果您在文件中定义了函数receiving/2,这意味着您有如下功能:

-module(ifelse).
-export([receiving/0,receiving/2]).

receiving() ->
    some_code.

receiving(A,B) ->
    other_code.
你可以称之为

Pid=spawn(如果是,则接收[1,2])

顺便说一下,在erlang中编写if语句是不常见的,原因是如果一个case与任何条件都不匹配,代码就会崩溃

5> F=fun(X) -> if (X rem 2) == 0 -> X+1 end end.
#Fun<erl_eval.6.82930912>
6> F(4).
5
7> F(5).
** exception error: no true branch found when evaluating an if expression
8> 
我在shell中对此进行了测试:

14> F = fun() ->                                        
14>     receive                                         
14>         {Pid,_A,_A} when is_pid(Pid) ->             
14>                 io:format(" B equals A ~n"),        
14>                 Pid ! "True";                       
14>         {Pid,_,_} when is_pid(Pid) ->               
14>                 io:format(" B does not equal A ~n"),
14>                 Pid ! "False";                      
14>         _ -> {error, wrong_value}                   
14>     end                                             
14> end.                                                
#Fun<erl_eval.20.82930912>
15> Pid = spawn(F).
<0.58.0>
16> Pid ! {self(),1,2}.
 B does not equal A 
{<0.51.0>,1,2}
17> % the returm value of "proc ! Mess" is Mess. It is what we get on the console on previous line
17> flush(). % use flush() to get the messages received by the shell
Shell got "False"
ok
18> Pid ! {self(),test,test}. % the process Pid is terminated now. when we send a message to it, 
18> % it is simply "lost".
{<0.51.0>,test,test}
19> % it is necessary though to use a new variable Pid1 and spawn a new process 
19> % (see rvirding message and user601836 answer)
19> Pid1 = spawn(F).         
<0.63.0>
20> Pid1 ! {self(),test,test}.
 B equals A 
{<0.51.0>,test,test}
21> flush().
Shell got "True"
ok
22> Pid2 = spawn(F).          
<0.68.0>
23> Pid2 ! {hello,test,test}. 
{hello,test,test}
24> flush(). 
ok
25> % of course there is no message sent back, no io:format to print something on the console,
25> % the returned value of the function in the error case is "lost".
25> % if you want to have a permanent process you must have a recursive loop, 
25> % calling receiving() were needed.
14>F=fun()->
14> 接收
14> {Pid,_A,_A}什么时候是_Pid(Pid)->
14> io:格式(“B等于A~n”),
14> Pid!“真实”;
14> {Pid,{,}什么时候是{Pid(Pid)->
14> io:格式(“B不等于A~n”),
14> Pid!“假”;
14> _u->{错误,错误的_值}
14> 结束
14> 结束。
#乐趣
15> Pid=繁殖(F)。
16> Pid!{self(),1,2}。
B不等于A
{,1,2}
17> %proc!Mess的returm值为Mess。这是我们在上一行的控制台上得到的
17> 刷新()。%使用flush()获取shell接收的消息
壳牌得到了“假”
好啊
18> Pid!{self(),test,test}.%进程Pid现在终止。当我们向它发送消息时,
18> 它只是“丢失了”。
{,测试,测试}
19> %但有必要使用新变量Pid1并生成新进程
19> %(请参阅rvirding消息和user601836应答)
19> Pid1=繁殖(F)。
20> Pid1!{self(),test,test}。
B等于A
{,测试,测试}
21>刷新()。
Shell说的是“真的”
好啊
22>Pid2=繁殖(F)。
23>Pid2!{你好,测试,测试}。
{你好,测试,测试}
24>刷新()。
好啊
25>%当然没有发送回消息,没有io:format在控制台上打印内容,
25>%错误情况下函数的返回值为“丢失”。
25>%如果要有一个永久进程,必须有一个递归循环,
25>%需要调用receiving()。

正如@rvirding所评论的那样,Erlang是单一赋值。您的问题可能与以下事实有关:您已经将一个值绑定到变量Pid,因此无法将任何新值绑定到它

只有在shell中(在实际代码中不推荐),才能使用f(variable)解除单个变量的绑定:

或者使用
f()
请注意,这仅用于测试目的

就我所知,您的代码是正确的,尽管我建议您使用
case
和模式匹配,而不是
if
语句

因此,我将重写您的代码,如下所示:

-module(ifelse).
-export([receiving/0]).

receiving() ->
    receive
        {Pid, A, B} ->
            case A =:= B of
                true ->
                    Pid ! "True";
                false ->
                    Pid ! "False"
            end
     end.
1> Pid = spawn(ifelse,receiving,[]).
<0.34.0>
2> ShellPid = self().
<0.32.0>
3> Pid ! {ShellPid, 4, 5}.
{0.32.0, 4, 5}
4> flush().
Shell got "False"
您可以按如下方式进行测试:

-module(ifelse).
-export([receiving/0]).

receiving() ->
    receive
        {Pid, A, B} ->
            case A =:= B of
                true ->
                    Pid ! "True";
                false ->
                    Pid ! "False"
            end
     end.
1> Pid = spawn(ifelse,receiving,[]).
<0.34.0>
2> ShellPid = self().
<0.32.0>
3> Pid ! {ShellPid, 4, 5}.
{0.32.0, 4, 5}
4> flush().
Shell got "False"
下面是如何测试它(在一个新的shell中,这样您就不必使用f()):

1>Pid=spawn(ifelse,receiving,[])。
2> ShellPid=self()。
3> Pid!{ShellPid,4,5}。
{0.32.0, 4, 5}
4> 刷新()。
Shell是假的
5> Pid!{ShellPid,4,4}。
{0.32.0, 4, 4}
6> 刷新()。
Shell实现了

正如@rvirding所评论的那样,Erlang是单一赋值。您的问题可能与以下事实有关:您已经将一个值绑定到变量Pid,因此无法将任何新值绑定到它

只有在shell中(在实际代码中不推荐),才能使用f(variable)解除单个变量的绑定:

或者使用
f()
请注意,这仅用于测试目的

1> Pid = spawn(ifelse,receiving,[]). <0.34.0> 2> ShellPid = self(). <0.32.0> 3> Pid ! {ShellPid, 4, 5}. {0.32.0, 4, 5} 4> flush(). Shell got false 5> Pid ! {ShellPid, 4, 4}. {0.32.0, 4, 4} 6> flush(). Shell got true