Erlang basics · Lesson 10 (open contents)
Compare terms and Boolean results
Use arithmetic, strict equality, term order, and short-circuit Boolean operators.
Run this first
Run it once, change one input, and compare the new result.
%% Compare numeric types and Boolean paths
{2 == 2.0, 2 =:= 2.0, true andalso false, false orelse true}.- The result is
{true,false,false,true}.
Read from the first line down
- Read
== / =:=: Compare numerically or exactly. - Read
andalso: Continue only when the left Boolean is true. - Read
orelse: Use the right Boolean only when the left is false.
Symbols are not secret signs
== / =:=Compare numerically or exactly.
andalsoContinue only when the left Boolean is true.
orelseUse the right Boolean only when the left is false.
Match each name to its meaning
exact equality
=:= requires the same value and numeric type; == allows integer-float equality.
term order
Erlang defines an order across term types, though application code should not depend on memorizing it.
short circuit
andalso and orelse evaluate the right side only when needed, and their conditions are Boolean.
Why these forms are useful
Run the example first. Predict one result, then change one input and run it again.
=:= requires the same value and numeric type; == allows integer-float equality.
Erlang defines an order across term types, though application code should not depend on memorizing it.
Close the answer and try
Check exact inequality and one short-circuit choice.
{3 ____ 3.0, false ____ true}.Target result: Return {true,true}.
Stuck? Read one hint
Use /= is not exact; use =/= and orelse.
After you run it, see one answer
{3 =/= 3.0, false orelse true}.Think about it: Does Erlang treat every non-false term as a Boolean?
No. Boolean operators expect the atoms true or false.
Remember these three lines
- 1
Use exact comparison when type matters.
- 2
Boolean operators need Boolean conditions.
- 3
Do not build business rules from the global term order.