Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang sytax error with 'Or'

Tags:

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

like image 747
Cheluis Avatar asked Dec 27 '22 13:12

Cheluis


2 Answers

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.
like image 164
Greg Hewgill Avatar answered Jan 09 '23 12:01

Greg Hewgill


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

like image 30
RichardC Avatar answered Jan 09 '23 12:01

RichardC