Compare terms and Boolean results: full examples and further reading
The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.
Use arithmetic, strict equality, term order, and short-circuit Boolean operators.
Run four comparisons and explain why 2 == 2.0 differs from 2 =:= 2.0.
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}.
Steps for the full example
- 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.
Original lesson notes
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.
Symbols and terms
== / =:=- Compare numerically or exactly.
andalso- Continue only when the left Boolean is true.
orelse- Use the right Boolean only when the left is false.
- 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
andalsoandorelseevaluate the right side only when needed, and their conditions are Boolean.
Original full exercise
Check exact inequality and one short-circuit choice.
{3 ____ 3.0, false ____ true}.Return {true,true}.
Show hint
Use /= is not exact; use =/= and orelse.
Show original answer
{3 =/= 3.0, false orelse true}.Does Erlang treat every non-false term as a Boolean?
No. Boolean operators expect the atoms true or false.
Original lesson takeaways
- Use exact comparison when type matters.
- Boolean operators need Boolean conditions.
- Do not build business rules from the global term order.