Erlang 语法错误,带有“Or”;
我在 erlang 中得到了这个非常新且简单的函数:
function_x(L) ->
X = lists:filter((fun(N)-> N =:= 2 end), L),
Y = lists:filter((fun(N)-> N =:= 3 end), L),
LX = length(X),
LY = length(Y),
LX == 2 or LY == 2.
编译源代码,然后出现此错误:
syntax error before: '=='
我从 or 子句中提取了其中一个表达式,它起作用了。正如你所见,我对 erlang 非常陌生,并且真的不明白为什么会发生这种情况,如果它看起来如此简单。有什么帮助吗?谢谢
I got this very newb and simple function in erlang:
function_x(L) ->
X = lists:filter((fun(N)-> N =:= 2 end), L),
Y = lists:filter((fun(N)-> N =:= 3 end), L),
LX = length(X),
LY = length(Y),
LX == 2 or LY == 2.
Compile the source, and I get this error:
syntax error before: '=='
I pull of one of the expressions from the or clausule and it works. I'm very newb in erlang as you see and really don't understand why this happens if it seems so simple. Any help? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于某种原因, == 和 'or' 可能具有相同的运算符优先级,因此您需要更准确地告诉编译器您想要什么。您可以写“(LX == 2) or (LY == 2)”或使用“orelse”而不是“or”。
For some reason, == and 'or' probably have the same operator precedence, so you need to tell the compiler more exactly what it is you want. You could either write "(LX == 2) or (LY == 2)" or use 'orelse' instead of 'or'.
根据Erlang中的运算符优先级,
或的优先级
高于==
。因此,您所写的表达式将被视为语法错误。要解决此问题,您必须在每个术语周围使用括号:
或者,您可以使用
orelse
,它的优先级低于==
:According to the operator precedence in Erlang, the precedence of
or
is higher than that of==
. So your expression as written is treated aswhich is a syntax error. To fix this, you must use parentheses around each term:
Alternatively, you can use
orelse
which has a lower precedence than==
: