Erlang receive..end中有多个匹配项用于查找的事例..of?
我在编译以下代码时遇到问题Erlang receive..end中有多个匹配项用于查找的事例..of?,erlang,switch-statement,syntax-error,Erlang,Switch Statement,Syntax Error,我在编译以下代码时遇到问题 2> c(match). match.erl:13: syntax error before: '{' match.erl:2: function receiver/0 undefined error match.erl -module(match). -export([receiver/0]). receiver() -> receive {From, A, B} -> case A =:= B
2> c(match).
match.erl:13: syntax error before: '{'
match.erl:2: function receiver/0 undefined
error
match.erl
-module(match).
-export([receiver/0]).
receiver() ->
receive
{From, A, B} ->
case A =:= B of
true ->
From ! "true";
false ->
From ! "false"
end
{From, A, B, C}->
case A =:= B =:= C of
true ->
From ! "true";
false ->
From ! "false"
end
end.
我试过在匹配前做所有可能的分号,句号,逗号,{From,A,B,C}->,但似乎没有任何效果。这就是Erlangs语法的噩梦 我认为,不可能像您在'A=:=B=:=C'中那样比较三个值。只比较其中的两个使您的代码可编译。请使用模式匹配
-module(match).
-export([receiver/0]).
receiver() ->
receive
{From, A, A} ->
From ! "true";
{From, _, _} ->
From ! "false";
{From, A, A, A}->
From ! "true";
{From, _, _, _}->
From ! "false"
end.
还是警卫
-module(match).
-export([receiver/0]).
receiver() ->
receive
{From, A, B} when A =:= B ->
From ! "true";
{From, _, _} ->
From ! "false";
{From, A, B, C} when A =:= B andalso A =:= C ->
From ! "true";
{From, _, _, _}->
From ! "false"
end.
or布尔运算符
-module(match).
-export([receiver/0]).
receiver() ->
receive
{From, A, B} ->
case A =:= B of
true ->
From ! "true";
false ->
From ! "false"
end;
{From, A, B, C}->
case A =:= B andalso A =:= C of
true ->
From ! "true";
false ->
From ! "false"
end
end.
我试着把这个A=:=B=:=C改成A=:=B,但实际上,重新定义同一个匹配案例两次都不起作用。在第一次结束后使用分号,并将A==B==C改为Imsteffan saidI所说的。多亏了Stavros Aronis的评论,我已经纠正了守卫示例:o