gpt4 book ai didi

erlang - 在接收..end中找到多个匹配项以查找..of?

转载 作者:行者123 更新时间:2023-12-03 08:13:34 25 4
gpt4 key购买 nike

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

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语法的噩梦!

最佳答案

请使用模式匹配

-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.

或 bool(boolean) 运算符
-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.

关于erlang - 在接收..end中找到多个匹配项以查找..of?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15396363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com