我在编译以下代码时遇到了问题,
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语法的噩梦!
答案 0 :(得分:5)
请使用模式匹配
-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.
或布尔运算符
-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.
答案 1 :(得分:2)
我认为不可能像你尝试做的那样比较三个值:A =:= B =:= C'。只比较其中两个可以使您的代码可编辑。