Erlang 编译器不断抛出奇怪的语法错误

标签 erlang syntax-error

所以,我正在 Erlang 中制作一个 tic-tac-toe 游戏,它不断抛出语法错误,我不知道为什么。它发生在检查 X 是否获胜的函数中。代码如下:

checkX(["xxx" | _]) -> true;
checkX([_ | ["xxx" | _]]) -> true;
checkX([_ | [_ | ["xxx" | _]]]) -> true;
checkX(Board) ->
    if 
        xy(Board, {0, 0}) == 120 and xy(Board, {0, 1}) == 120 and xy(Board, {0, 2}) == 120 -> true;

        xy(Board, {1, 0}) == 120 and xy(Board, {1, 1}) == 120 and xy(Board, {1, 2}) == 120 -> true;

        xy(Board, {2, 0}) == 120 and xy(Board, {2, 1}) == 120 and xy(Board, {2, 2}) == 120 -> true;

        xy(Board, {0, 0}) == 120 and xy(Board, {1, 1}) == 120 and xy(Board, {2, 2}) == 120 -> true;

        xy(Board, {2, 0}) == 120 and xy(Board, {1, 1}) == 120 and xy(Board, {0, 2}) == 120 -> true;

       true -> false

    end.

(xy/2 是一个函数,它接受一个板(它是一个字符串列表)和一个元组,并返回元组中该位置的字符。)当我编译它:

board.erl:68: syntax error before '=='

(第 68 行是 if 语句中的第一个条件。)

有人知道这是为什么吗?

最佳答案

and== 绑定(bind)更紧密。以下是您的保护中的隐式括号:

xy(Board, {0, 0}) == (120 and xy(Board, {0, 1}))

这不是你想要的。你的整个守卫最终类似于:

if 
   Board == 2 == 3 -> true;
   true -> false
end

会产生相同的错误。您可以使用一些括号修复错误:

if 
   (Board == 2) == 3 -> true;
   true -> false
end

但是,即使您为守卫添加括号,您的守卫也会遇到下一个错误。请参阅erlang guards在文档中。也就是说,你不能只调用守卫中的任何函数——守卫中只能调用有限数量的函数,并且文档中列出了允许的函数。

我建议您在编写防护时从 , 而不是 and 开始,然后根据需要进行调整,例如andalso

关于Erlang 编译器不断抛出奇怪的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60637744/

相关文章:

if-statement - 如何在 Erlang 中等效于 C 风格的 if-else 语句?

erlang - OTP应用程序:load doesn't load a new compiled code

erlang - 无法删除我知道存在的 mnesia 表

sql - PLS-00103 : Encountered the symbol when expecting one of the following:

python - 使用Python编写MakeHuman文件: Invalid Syntax Error [closed]

erlang - 主管启动多个子项作为原子操作

logging - RabbitMQ SASL 日志记录

javascript - 如何在 JavaScript 中找到所有 2 对具有相同乘积的整数?

c++ - 在 C++ 的 while 循环中使用 pow() 函数

python - 为什么会收到此SyntaxError?