jeffreyveon
jeffreyveon

Reputation: 13830

Erlang compound boolean expression

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 ==".

Upvotes: 4

Views: 6094

Answers (4)

cthulahoops
cthulahoops

Reputation: 3835

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.

Upvotes: 8

markmywords
markmywords

Reputation: 663

Without braces you can also do

X = 15.
Y = 20.
X == 15 andalso Y == 20.

Upvotes: 5

jldupont
jldupont

Reputation: 96716

Try:

X = 15.
Y = 20.
(X==15) and (Y==20).

Upvotes: 10

Related Questions