← Back to lesson 10: Compare terms and Boolean results

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.

Original full example
%% 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

  1. Read == / =:=: Compare numerically or exactly.
  2. Read andalso: Continue only when the left Boolean is true.
  3. 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
andalso and orelse evaluate the right side only when needed, and their conditions are Boolean.

Original full exercise

Check exact inequality and one short-circuit choice.

Exercise starter
{3 ____ 3.0, false ____ true}.

Return {true,true}.

Show hint

Use /= is not exact; use =/= and orelse.

Show original answer
Reference 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
Design trade-offs and language comparisons
Original course references