tags:

views:

170

answers:

4

I am still finding my feet with erlang, and I read the documentation about using "and", "or" operators, but why is the following not evaluating?

X = 15,
Y = 20,
X==15 and Y==20.

I am expecting for a "true" in the terminal, but I get a "syntax error before ==".

+5  A: 

Try:

X = 15.
Y = 20.
(X==15) and (Y==20).
jldupont
+4  A: 

Without braces you can also do

X = 15.
Y = 20.
X == 15 andalso Y == 20.
markmywords
+4  A: 

You probably don't want to use and. There are two problems with and, firstly as you've noticed its precedence is strange, secondly it doesn't short circuit its second argument.

1> false and exit(oops).
** exception exit: oops
2> false andalso exit(oops).
false

andalso was introduced later to the language and behaves in the manner which is probably more familiar. In general, use andalso unless you have a good reason to prefer and.

Btw, orelse is the equivalent of or.

cthulahoops
It really depends on whether you are using it for control or boolean tests. Using and/or has the benefit of testing the return values of the "boolean" operations.
rvirding
Ok, thanks for that insight. Got a better picture now.
jeffreyveon