开发者

Erlang sytax error with 'Or'

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 unders开发者_运维百科tand why this happens if it seems so simple. Any help? Thanks


According to the operator precedence in Erlang, the precedence of or is higher than that of ==. So your expression as written is treated as

LX == (2 or LY) == 2

which is a syntax error. To fix this, you must use parentheses around each term:

(LX == 2) or (LY == 2).

Alternatively, you can use orelse which has a lower precedence than ==:

LX == 2 orelse LY == 2.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜