0

我在编译以下代码时遇到问题,

2> c(match).
match.erl:13: syntax error before: '{'
match.erl:2: function receiver/0 undefined
error

匹配.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 的语法是一场噩梦的地方!

4

2 回答 2

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.
于 2013-03-13T21:19:07.187 回答
2

我相信不可能像您尝试在“A =:= B =:= C”中那样比较三个值。仅比较其中两个使您的代码可编译。

于 2013-03-13T21:16:31.550 回答